Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Can the following three separate update commands be combined into just once command?UPDATE Mytable SET Fld1 = '' WHERE Fld1 IS NULLUPDATE Mytable SET Fld2= '' WHERE Fld2 IS NULLUPDATE Mytable SET Fld3= '' WHERE Fld3 IS NULLWhat would be the optimal single command?Thanks!--PhB
Lamprey
Master Smack Fu Yak Hacker
4614 Posts
Posted - 2014-05-16 : 18:29:16
Something like this should work:
UPDATE Mytable SET Fld1 = COALESCE(Fld1, '') ,Fld2= COALESCE(Fld2, '') ,Fld3= COALESCE(Fld3, '') WHERE Fld1 IS NULL OR Fld2 IS NULL OR Fld3 IS NULL
Lamprey
Master Smack Fu Yak Hacker
4614 Posts
Posted - 2014-05-16 : 18:33:43
As a side note, depending on indexing and other factors, individual queries might be faster. Having OR predicates sometimes lead to table scans. So, just putting it all into one statement might not be the most efficient, if that is/was your goal.