Solved

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

Posted on 2011-09-07
2
281 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
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PL/SQL query 14 46
SQL server 2008 SP4 29 33
SQL Script to find duplicates 16 19
Row-Level Security 2 19
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now