So lets look at what you are doing, you are doing an UPDATE and a bunch of IF-THEN-ELSE statements.
In (MSSQL) SQL that translates to
- Code: Select all
UPDATE tableName
SET columName = <expression>, [ ...n ]
WHERE <conditions>
And to use an IF-THEN-ELSE construct in SQL you can use the following syntax:
- Code: Select all
CASE
WHEN <condition> THEN <value>
[ ...n ]
[ ELSE <value> ]
END
To trim a string you use LTRIM() and RTRIM(), which does what it suggests.
The substr function will translate to SUBSTRING(value, start, length).
And the utils.stringPatternCount() function translates to CHARINDEX(searchValue,value), well at least the way you are using it in your method. CHARINDEX() returns the position of the string, not the count.
So this will give you all the ingredients you need for the update query.
Of course there is one bit in your code that we don't know and that is the related value that is also a calc. Is this a stored calc and what is the join criteria ?
Anyway, the following (untested) SQL is a translation of what you are doing in your method.
- Code: Select all
UPDATE tableName
SET
skmarq = LTRIM(RTRIM(semarq))
,canalcde = CASE
WHEN semarq IS NOT NULL THEN
CASE
WHEN SUBSTRING(semarq, 1, 3) = 'WEB' THEN 2
WHEN SUBSTRING(semarq, 1, 3) = 'SCE' THEN 7
WHEN SUBSTRING(semarq, 1, 3) = 'TEL' THEN 9
WHEN SUBSTRING(semarq, 1, 4) = 'SRCE' THEN 8
WHEN SUBSTRING(semarq, 1, 4) = 'MAIL' THEN 1
WHEN SUBSTRING(semarq, 1, 3) = 'COM' OR semarq IN ('BGNW','DIET','RIVIERE','RIVEBONTE','SERSOUB','SALGUES') THEN 10
ELSE null -- assuming this will do in case of a non-match
END
ELSE
CASE
WHEN t7t10$ = 'TEL' THEN
CASE
WHEN CHARINDEX('SCE',skstat) > 0 THEN 7
WHEN CHARINDEX('SRC',skstat) > 0 THEN 8
WHEN CHARINDEX('CO',skstat) > 0 OR CHARINDEX('PW',skstat) > 0 THEN 9
ELSE 3
END
WHEN CHARINDEX('SRC',skstat) > 0 THEN 8
WHEN CHARINDEX('WEB',skstat) > 0 THEN 2
WHEN CHARINDEX('LIB',skstat) > 0 THEN 4
WHEN CHARINDEX('VC20',skstat) > 0 OR SUBSTRING(skstat,1,2) = '20' THEN 5
WHEN (t7t01$ = 'M' OR t7t10$ = 'M') AND NOT (t7t10$ = 'TEL') THEN 1
ELSE 6
END
END
,ca_ht = ((skqte * skmont) - (skqte * skmont * skrem / 100 )) * skaf
,net_ann = CASE
WHEN skcep IN ('01','30','24') THEN '1'
WHEN skcep IN ('19','20') THEN '0'
ELSE '2'
END
-- calc and related field, don't know the criteria for both. Using pseudo code for the columns here.
,recap = CASE
WHEN related.publication_id IS NOT NULL THEN related.recap
ELSE '0'
END
Do test the SQL in a real SQL editor (and probably with a WHERE clause so you only test on 1 or a few records).
Hope this gets you going.