Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article I will describe the Copy Database Wizard 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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

930 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

9 Experts available now in Live!

Get 1:1 Help Now