Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, All

i'm facing problem with this query, i want this query when return empty then i want fill value 0,

My Query Is:

SQL
select distinct sm.EmpCodeC,sm.EmpNameC,(select count(UserId) from CanteenLogs where TimeEntry between '01/Oct/2014 09:00:00 AM' and '01/Oct/2014 10:00:00 AM' and userId='20') as 'BreakFast' from CanteenLogs as rd inner join StaffMaster as sm on rd.UserID=sm.UserIdN and rd.UserID='20'


Here i'm passing value 20, if userid=20 returns empty i want BreakFast column as value 0
Posted
Updated 27-Nov-14 23:59pm
v2

Use COALESCE[^] function, which replaces NULL with passed value.
SQL
SELECT COALESCE(NULL, 0) --returns 0
 
Share this answer
 
check this

SQL
select distinct 
   sm.EmpCodeC,sm.EmpNameC,
  Isnull((select count(UserId) from CanteenLogs where TimeEntry between '01/Oct/2014 09:00:00 AM' and '01/Oct/2014 10:00:00 AM' and userId='20'),0) as 'BreakFast' 
from CanteenLogs as rd inner join StaffMaster as sm on rd.UserID=sm.UserIdN and rd.UserID='20


and this when no record returned

SQL
select distinct
   sm.EmpCodeC,sm.EmpNameC,
  Isnull((select count(UserId) from CanteenLogs
                 where TimeEntry between '01/Oct/2014 09:00:00 AM' and '01/Oct/2014 10:00:00 AM' and userId='20'),0) as 'BreakFast'
 into #tempfile
from CanteenLogs rd inner join StaffMaster sm on rd.UserID=sm.UserIdN and rd.UserID='20'

IF Exists (select top 1 * from #tempfile)
select * from Outputdata
else
select '' EmpCodeC ,'' EmpNameC ,0 BreakFast
 
Share this answer
 
v3
Comments
prasanna.raj 28-Nov-14 6:06am    
its return empty ...
prasanna.raj 28-Nov-14 6:08am    
i need return value 0, so that where i want use 0.
Shweta N Mishra 28-Nov-14 6:12am    
does there no record return for your query or just 'BreakFast' column is empty
prasanna.raj 28-Nov-14 6:15am    
it possible to set value 0, when return column is empty.
Shweta N Mishra 28-Nov-14 6:21am    
if there is no record return then not,but there is other way to do that. first can you answer my question do you get any record out of your selection even null ?

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