Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to handled Nulls and empty values in sql server


how to handled Nulls and empty values in sql server

Author
Message
asranantha
asranantha
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: 4717
Hi friends i have small doubt in sql server how to handled 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.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16616 Visits: 17024
asranantha (10/18/2013)
Hi friends i have small doubt in sql server how to handled 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.


This is a little odd given the data you presented. Here are a couple of ways you do it.


select isnull(nullif('', YourColumn), 'NA') as YourColumn,
case when YourColumn IS NULL OR YourColumn = '' then 'NA' else YourColumn end as YourColumn



Here is the problem. The data you posted appears to be ints. You can't mix int and varchar data in the same column unless you cast your numeric data to a varchar. If at all possible I would do this type of formatting in the front end instead of in sql.

_______________________________________________________________

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 Moden's 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)
jonysuise
jonysuise
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 166
Hi asranantha, :-D

I would use COALESCE AND CASE, because it's SQL standard.

SELECT CASE COALESCE(your_column,'NA') WHEN '' THEN 'NA' ELSE your_column END



Hope it helps. Cool

Jonathan Bernardez Bernardez
___________________________________________________________
DBD. MCSA SQL Server 2012
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45147 Visits: 39923
jonysuise (10/18/2013)
Hi asranantha, :-D

I would use COALESCE AND CASE, because it's SQL standard.



I would use just ISNULL AND NULLIF (like Sean did) because you don't have unexpected datatype precedence problems with ISNULL like you can with COALESCE, is a tiny bit faster than COALESCE, takes fewer characters to type, and it ticks all sorts of people off that still think SQL is portable across engines. :-D

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
jonysuise
jonysuise
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 166
Jeff Moden (10/18/2013)
jonysuise (10/18/2013)
Hi asranantha, :-D

I would use COALESCE AND CASE, because it's SQL standard.



I would use just ISNULL AND NULLIF (like Sean did) because you don't have unexpected datatype precedence problems with ISNULL like you can with COALESCE, is a tiny bit faster than COALESCE, takes fewer characters to type, and it ticks all sorts of people off that still think SQL is portable across engines. :-D


Thanks Jeff. It's a matter of opinion i think. :-D

Jonathan Bernardez Bernardez
___________________________________________________________
DBD. MCSA SQL Server 2012
UmaShankar Patel
UmaShankar Patel
SSC Veteran
SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)

Group: General Forum Members
Points: 288 Visits: 337
Check this post
handling-null-values-in-sql-server

Thanks.......
-----------------------------------
My Blog | Articles
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45147 Visits: 39923
jonysuise (10/19/2013)
Jeff Moden (10/18/2013)
jonysuise (10/18/2013)
Hi asranantha, :-D

I would use COALESCE AND CASE, because it's SQL standard.



I would use just ISNULL AND NULLIF (like Sean did) because you don't have unexpected datatype precedence problems with ISNULL like you can with COALESCE, is a tiny bit faster than COALESCE, takes fewer characters to type, and it ticks all sorts of people off that still think SQL is portable across engines. :-D


Thanks Jeff. It's a matter of opinion i think. :-D


Actually, if you make a nice little million row table, you can prove to yourself the slight performance gain. You can also prove to yourself that, if you don't mind you're P's and Q's, the data precedence thing can really mess up performance. ;-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
jonysuise
jonysuise
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 166
Jeff Moden (10/19/2013)
jonysuise (10/19/2013)
Jeff Moden (10/18/2013)
jonysuise (10/18/2013)
Hi asranantha, :-D

I would use COALESCE AND CASE, because it's SQL standard.



I would use just ISNULL AND NULLIF (like Sean did) because you don't have unexpected datatype precedence problems with ISNULL like you can with COALESCE, is a tiny bit faster than COALESCE, takes fewer characters to type, and it ticks all sorts of people off that still think SQL is portable across engines. :-D


Thanks Jeff. It's a matter of opinion i think. :-D


Actually, if you make a nice little million row table, you can prove to yourself the slight performance gain. You can also prove to yourself that, if you don't mind you're P's and Q's, the data precedence thing can really mess up performance. ;-)


Check this post :

http://sqlmag.com/t-sql/coalesce-vs-isnull

As i said, it's a matter of opinion. I don't think it's correct to point a solution as better than the other one.. just be aware of the pros and cons of each one of them and choose the one that fit your needs. :-)

Jonathan Bernardez Bernardez
___________________________________________________________
DBD. MCSA SQL Server 2012
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