How can I update multiple records in a database using one Update statement where the cases are different.
refundNumber = CASE _
WHEN salesRecords.invNo='1' AND itemNo='250' AND length(refundNumber) > 1 THEN _
concat(refundNumber, ', 88' ) Else '88' _
WHEN salesRecords.invNo='1' AND itemNo='7095' AND length(refundNumber) > 1 THEN _
concat(refundNumber, ', 88' ) Else '88' _
END
This fails and should look like:
refundNumber = CASE _
WHEN invNo='1' AND itemNo='250' AND length(refundNumber) > 1 THEN _
concat(refundNumber, ', 88' ) _
WHEN invNo='1' AND itemNo='7095' AND length(refundNumber) > 1 THEN _
concat(refundNumber, ', 88' ) Else '88' _
END
The difference being that the Else comes right at the end.
But it sets refundNumber on ALL records to 88.
What I am trying to do is add a value to a field named refundNumber to those items where the invNo = 'currentInvoiceNumber' AND itemNo = 'itemNumber'
BUT, since refundNumber is a text field which is meant to contain a comma separated list, I am trying to determine whether this field is empty or not, if it is, just enter the number, if it isn’t, append the number preceded by a comma to the existing field content. Hence the length(refundNumber) > 1 THEN concat(refundNumber, ', 88' ) bit.
I’ve not tested it, but I believe the VB logic I’d need would be:
IF invNo='1' And itemNo='250' THEN
IF length(refundNumber) > 1 THEN
concat(refundNumber, ', 88' )
ELSE
'88'
END IF
ELSE IF invNo='1' And itemNo='7095' THEN
IF length(refundNumber) > 1 THEN
concat(refundNumber, ', 88' )
ELSE
'88'
END IF
Else
refundNumber '- leave the field value as is because it does not comply with any of the above conditions
END IF
How could I translate this to an SQL CASE string?
If you don’t want to update records where the InvNo is not ‘1’ and the itemNo is not ‘250’ or ‘7095’ why not have a where clause limiting the set to this criteria? Then all you need to do concatenate refund number and 88 using a case expression, but you don’t even need this you could use:
Or use a nested case expression?