Click here to Skip to main content
15,898,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to get the value as 0 instead of null in select query if the result value is null.
Posted

Use this in your select statement :

VB
CASE WHEN FieldName IS NULL OR FieldName=''
   THEN 0
   Else FieldName   
   End


Pls, Accept as answer and vote if solve your problem.
 
Share this answer
 
Comments
Maheswar Tripathy 12-Jul-13 3:33am    
thanks for answer but it will not working getting an error....
jaideepsinh 12-Jul-13 3:40am    
Can you paste you query here then i can modify as per your need.
Maheswar Tripathy 12-Jul-13 3:45am    
there is table named as salary and columns are name and ctc

select case when ctc is null then 0 else ctc end from salray where name =''
jaideepsinh 12-Jul-13 3:52am    
This query is correct.Can you explain what error it throw.
Maheswar Tripathy 12-Jul-13 4:09am    
Conversion failed when converting the varchar value 'null' to data type int.
Try this ...:)

SQL
SELECT ISNULL(CAST(NULLIF(ColumnName, 'NULL') AS int), 0)
FROM TableName
 
Share this answer
 
v2
Comments
Maheswar Tripathy 12-Jul-13 3:46am    
Dear Nirav

thanks for the answer but it gives error
Nirav Prabtani 12-Jul-13 3:47am    
what is d error???
Maheswar Tripathy 12-Jul-13 4:10am    
Conversion failed when converting the varchar value 'null' to data type int.
Nirav Prabtani 12-Jul-13 4:39am    
try my updated solution...:)
Use this. Hope it helps

ISNULL(FIELD_NAME,0)
 
Share this answer
 
Comments
Maheswar Tripathy 12-Jul-13 3:32am    
aleady use this but getting an error "conversion failed while converting var-char null to 0"
ArunRajendra 12-Jul-13 4:07am    
Then try this way ISNULL(@v,'0').
Try this:

SQL
SELECT CASE WHEN columnName IS NULL THEN '0' ELSE columnName END FROM tableName 
 
Share this answer
 
v2
Comments
Maheswar Tripathy 12-Jul-13 4:22am    
getting an error ""Conversion failed when converting the varchar value 'null' to data type int.""
berrymaria 12-Jul-13 4:26am    
Is your columnName a varchar? or what is the data type of it?
Hi,
try this.

select case when ctc is null or ctc='' then '0' else ctc end from salary where name ='XYZ'


Note: Null is a varchar kind of value while 0 is int , that's why its giving error when you are writing
"case when ctc is null then 0 else ctc end"

so use it Like this

"case when ctc is null then '0' else ctc end"

Hope it will help you.. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900