Solved

SQL syntax:  update multiple fields in the same table with different conditions

Posted on 2011-09-07
2
288 Views
Last Modified: 2012-05-12
is there a way to combine these statements?

update table set field1 = 0 where field1 is NULL
update table set field2 = 0 where field2 is NULL


field1 and field2 are in the same table.  however, there are instances where field1 may be NULL but field2 is not NULL (or vice versa), and i don't want to update field1 to zero when field2 is NULL.

thanks
0
Comment
Question by:zephyr_hex (Megan)
2 Comments
 
LVL 42

Accepted Solution

by:
dqmq earned 250 total points
ID: 36499281
update table
 set field1 = isnull(field1,0)
, field2 = isnull(field2,0)
0
 
LVL 18

Assisted Solution

by:lludden
lludden earned 250 total points
ID: 36499291
Update Mytable SET Field1 = CASE WHEN Field1 IS NULL THEN 0 ELSE Field1 END
Field2 - CASE WHEN Field2 IS NULL THEN 0 ELSE Field2 END
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question