Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to use the statement below to only update null or blank fields. Right now the way that it is set up is if it matches then up date the matched field whether or not its blank or null.

Merge into table1 as T
using [table] as S
on T.[Last Name] = S.[Last Name] and T.[First Name] = S.[First Name]

When Matched then 
Update Set T.[middle name] = S.[middle];


If there is a simple way to get this result I would be happy to see it.

What I have tried:

tried to use t.[middle name] is null, but its not exactly working the way I want it to.
Posted
Updated 10-Dec-17 16:15pm
Comments
Oshtri Deka 10-Dec-17 22:10pm    
Please explain how does it not work?

1 solution

Try this:

SQL
Merge into table1 as T
using [table] as S
on T.[Last Name] = S.[Last Name] and T.[First Name] = S.[First Name]
and (T.[middle name] is null or TRIM(T.[middle name]) = '')
 
When Matched then 
Update Set T.[middle name] = S.[middle];
 
Share this answer
 
Comments
CHill60 11-Dec-17 4:52am    
I think I'd prefer and TRIM(ISNULL(T.[middle name],'')) = '' to having the OR - parentheses are often missed off when mixing ANDs and ORs
Oshtri Deka 12-Dec-17 5:39am    
Valid point, your approach is more concise.
When it comes to parentheses, every coder should pay attention.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900