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


handling null or empty values useing update statement in sql server


handling null or empty values useing update statement in sql server

Author
Message
asranantha
asranantha
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1046 Visits: 4717
Hi friends i have small doubt in sql server how to update nulls and empty values and replac other values in sql server

table data look like below
table :emp
id ,name ,sal ,deptno
1 ,abc ,1oo ,10
2 ,venu ,2000 ,null
3 ,null , ,20
4 ,balu ,null ,
5 ,hari , ,30

based on above table i want output like below

id , name ,sal ,deptno
1 , abc ,100 ,10
2 , venu ,2000 ,NA
3 , NA , NA ,20
4 , balu ,NA ,NA
5 , hari ,NA ,30

plese tell me how to write query while achive in above issuse useing update query
actualy i tried like below query but its not give above exactly result

update emp set name=(case isnull(name,'NA') when '' then 'NA'else name end),
sal =(case isnull(sal,'NA')when '' then 'NA' else sal end) ,
deptno=(case isnull(desptno,'NA') when '' then 'NA' else deptno end)
from emp.

here its not given exactely output.plese tell me update query in sql server.
jonysuise
jonysuise
Old Hand
Old Hand (312 reputation)Old Hand (312 reputation)Old Hand (312 reputation)Old Hand (312 reputation)Old Hand (312 reputation)Old Hand (312 reputation)Old Hand (312 reputation)Old Hand (312 reputation)

Group: General Forum Members
Points: 312 Visits: 166
asranantha (10/19/2013)
Hi friends i have small doubt in sql server how to update nulls and empty values and replac other values in sql server

table data look like below
table :emp
id ,name ,sal ,deptno
1 ,abc ,1oo ,10
2 ,venu ,2000 ,null
3 ,null , ,20
4 ,balu ,null ,
5 ,hari , ,30

based on above table i want output like below

id , name ,sal ,deptno
1 , abc ,100 ,10
2 , venu ,2000 ,NA
3 , NA , NA ,20
4 , balu ,NA ,NA
5 , hari ,NA ,30

plese tell me how to write query while achive in above issuse useing update query
actualy i tried like below query but its not give above exactly result

update emp set name=(case isnull(name,'NA') when '' then 'NA'else name end),
sal =(case isnull(sal,'NA')when '' then 'NA' else sal end) ,
deptno=(case isnull(desptno,'NA') when '' then 'NA' else deptno end)
from emp.

here its not given exactely output.plese tell me update query in sql server.


Hi asranantha, :-D

I would use this query :


update emp
set name = CASE WHEN coalesce(name,'') = '' THEN 'NA' ELSE name END,
sal = CASE WHEN coalesce(sal,'') = '' THEN 'NA' ELSE sal END,
deptno = CASE WHEN coalesce(deptno,'') = '' THEN 'NA' ELSE deptno END



Hope it helps ! Cool

Jonathan Bernardez Bernardez
___________________________________________________________
DBD. MCSA SQL Server 2012
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64189 Visits: 17974
Please don't post multiple threads for the same thing. See the original thread here. http://www.sqlservercentral.com/Forums/Topic1506273-391-1.aspx

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
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