Solved

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

Posted on 2011-09-07
2
292 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)
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

752 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