SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update to 0 if NULL


Update to 0 if NULL

Author
Message
facticatech
facticatech
SSC Veteran
SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)

Group: General Forum Members
Points: 216 Visits: 146
I have set of columns (more than 10) and need to update them to 0 if it is null.

for eg: update table set column1=0 where column1 is null
update table set column2=0 where column2 is null
............
............
............
update table set column10=0 where column10 is null

Is there a better way to write in a single statement instead of 10 statements like the one above for 10 different columns?
Michael Valentine Jones
Michael Valentine Jones
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14785 Visits: 11848


update MyTable
set
column1 = case when column1 is null then 0 else column1 end,
column2 = case when column2 is null then 0 else column2 end,
... and so on ...
column10 = case when column10 is null then 0 else column10 end
where
column1 is null or
column2 is null or
... and so on ...
column10 is null



Dave Ballantyne
Dave Ballantyne
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7274 Visits: 8370
facticatech (1/12/2011)


Is there a better way to write in a single statement instead of 10 statements like the one above for 10 different columns?



"Better" is always subjective , and it does depend.

In your case, you could have a 10million row table where 3 rows match the condition "Where ColumnX is null", if you had a usable index on those columns then your original multi statement method would be "better".

However , i would imagine that here it is not the case here and a single scan would be preferable, though i would use ISNULL the effect is the same .

Just a little food for thought :-)



Clear Sky SQL
My Blog
facticatech
facticatech
SSC Veteran
SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)

Group: General Forum Members
Points: 216 Visits: 146
Better in terms of performance only. The table has no indexes and is mainly used for writing and less selectivity.
roi.reuven
roi.reuven
Old Hand
Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)

Group: General Forum Members
Points: 326 Visits: 195
Many indexes can slow down the update queries and the insert queries is this the reason that no indexes on the table
roi.reuven
roi.reuven
Old Hand
Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)

Group: General Forum Members
Points: 326 Visits: 195
You can also write this query it to tell you what are the missing indexes
This will work only on db witch people working and apps are sending queries to this db

select * from sys.dm_db_missing_index_details
psingla
psingla
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1418 Visits: 1249
facticatech (1/12/2011)
I have set of columns (more than 10) and need to update them to 0 if it is null.

for eg: update table set column1=0 where column1 is null
update table set column2=0 where column2 is null
............
............
............
update table set column10=0 where column10 is null

Is there a better way to write in a single statement instead of 10 statements like the one above for 10 different columns?


hopefully this is what you are looking for

Update table
Set col1=isnull(col1,0),
Col2=isnull(col2,0),
Col3=isnull(col3,0),
….
….
Col10=isnull(col10,0)

Pramod
SQL Server DBA | MCSE SQL Server 2012/2014

in.linkedin.com/in/pramodsingla/
http://pramodsingla.wordpress.com/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search