I’m performing a MERGE statement from a temp table t into the main table ts, matching on an ID field. It contains the following:
WHEN MATCHED THEN UPDATE
SET ts.username = t.username, ts.password = t.password, ts.title = t.title, ts.firstname = t.firstname, ts.surname = t.surname, ts.email = t.email
If the username and password is blank in the temp table t, I don’t want to update the main table ts. How can I do this?
EDIT: Seems like a CASE statement or using COALESCE(NULLIF(t.username, ''), ts.username) will work. Which is best?
When dealing with blanks (i.e. empty string), then using a case statement
If you’re dealing with null, then use isnull
If it can be either blank or null, you can deal with both in a case statement