Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
crugerenator
Posting Yak Master
126 Posts |
Posted - 2008-09-04 : 17:45:11
|
I am trying to make nulls return as zeros, and if a value is not null then I want it to return regularly.For example:select empID, empName, salary from employee left join compensation on employee.empID = compensation.empIDHow would I make salary return as 0 instead of null if there was no salary in the compensation table for an employee, but return as the correct salary if there is a salary in the compensation table for that employee?Thanks |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-09-04 : 17:48:38
|
try the replace functionhttp://msdn.microsoft.com/en-us/library/ms186862.aspx |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-09-04 : 17:52:49
|
my mistakequote: Originally posted by tkizer You can use COALESCE or ISNULL functions. Check SQL Server Books Online for details such as syntax.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
better option |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
shanmp
Starting Member
13 Posts |
Posted - 2008-09-05 : 04:13:28
|
Please see the below query , I have used case statement hope it will work fine. select select empID,empName,case when salary = 0 then null else salary end as salary from employeeleft join compensationon employee.empID = compensation.empID |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-09-05 : 04:17:44
|
That should beselect select empID,empName,case when salary is null then 0 else salary end as salary from employeeleft join compensationon employee.empID = compensation.empIDorselect select empID,empName,coalesce(salary ,0) as salary from employeeleft join compensationon employee.empID = compensation.empIDMadhivananFailing to plan is Planning to fail |
|
|
|
|
|
|
|