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


IF NULL then Zero (0)


IF NULL then Zero (0)

Author
Message
EjSQLme
EjSQLme
SSC-Enthusiastic
SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)

Group: General Forum Members
Points: 182 Visits: 431
Hi,

Using the sample data below, How would I convert Null to zero (0) if the count is NULL with out creating a temp table?


CREATE TABLE #Table_Temp
(ProdID INT
,StatusID INT
,CreatedDate DATETIME)

INSERT INTO #Table_Temp
Select 2063, 1, '2009-10-18' Union
Select 1018, 2, '2009-05-10' Union
Select 3013, 3, '2010-01-15' Union
Select 1753, 1, '2009-09-01' Union
Select 3588, 3, '2010-06-07' Union
Select 674, 2, '2009-04-06' Union
Select 1037, 3, '2009-05-29' Union
Select 1250, 3, '2009-06-15' Union
Select 504, 3, '2009-01-18' Union
Select 3321, 3, '2010-03-24' Union
Select 1754, 1, '2009-09-01' Union
Select 528, 3, '2009-02-01' Union
Select 3712, 3, '2010-07-01' Union
Select 3592, 3, '2010-06-07' Union
Select 3446, 1, '2010-05-03' Union
Select 3421, 3, '2010-04-26' Union
Select 2962, 2, '2010-01-09' Union
Select 507, 3, '2009-01-23' Union
Select 3369, 3, '2010-04-14' Union
Select 3233, 3, '2010-03-10'


select * from #Table_Temp order by CreatedDate

select
'ActiveProd' = (select count(*)
from #Table_Temp TT1
where StatusID = 1
and dateadd(month, datediff(month, 0, tt1.CreatedDate),0) = dateadd(month, datediff(month, 0, tt.CreatedDate),0)
group by dateadd(month, datediff(month, 0, CreatedDate),0)),
'InactiveProd' = (select count(*)
from #Table_Temp TT1
where StatusID = 2
and dateadd(month, datediff(month, 0, tt1.CreatedDate),0) = dateadd(month, datediff(month, 0, tt.CreatedDate),0)
group by dateadd(month, datediff(month, 0, CreatedDate),0)),
'DiscontinutedProd' = (select count(*)
from #Table_Temp TT1
where StatusID = 3
and dateadd(month, datediff(month, 0, tt1.CreatedDate),0) = dateadd(month, datediff(month, 0, tt.CreatedDate),0)
group by dateadd(month, datediff(month, 0, CreatedDate),0)),
(convert(varchar(4),dateadd(month, datediff(month, 0, CreatedDate),0),100) + convert(varchar(4),year(dateadd(month, datediff(month, 0, CreatedDate),0)))) as ProdCountDT
from #Table_Temp TT
where dateadd(month, datediff(month, 0, CreatedDate),0) between getdate() - 365 and getdate()
group by dateadd(month, datediff(month, 0, CreatedDate),0)
order by dateadd(month, datediff(month, 0, CreatedDate),0)

drop table #Table_Temp


jvanderberg
jvanderberg
Say Hey Kid
Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)

Group: General Forum Members
Points: 708 Visits: 746
ISNULL() does exactly that:

ISNULL([Field],0)


This returns 0 if [Field] is NULL, otherwise it will return [Field].

But I prefer COALESCE() because it is a little more flexible:

COALESCE([Field],0)


The above code does exactly the same thing as ISNULL(). But COALESCE() can do a little bit more:

COALESCE([Field1],[Field2],[Field3],...,0)


COALESCE() scans through each parameter in order, returning the first non-null one. In this case I added 0 at the end to catch an situation where all of the fields are NULL.
Cadavre
Cadavre
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3984 Visits: 8472
Isnull is your friend :-)

SELECT 'ActiveProd' = Isnull((SELECT COUNT(*)
FROM #table_temp tt1
WHERE statusid = 1
AND Dateadd(MONTH, Datediff(MONTH, 0,
tt1.createddate), 0) =
Dateadd(MONTH, Datediff(MONTH, 0,
tt.createddate), 0)
GROUP BY Dateadd(MONTH, Datediff(MONTH, 0,
createddate),
0)), 0),
'InactiveProd' = Isnull((SELECT COUNT(*)
FROM #table_temp tt1
WHERE statusid = 2
AND Dateadd(MONTH, Datediff(MONTH, 0,
tt1.createddate), 0) =
Dateadd(MONTH, Datediff(MONTH, 0,
tt.createddate), 0)
GROUP BY Dateadd(MONTH, Datediff(MONTH, 0,
createddate
), 0)), 0),
'DiscontinutedProd' = Isnull((SELECT COUNT(*)
FROM #table_temp tt1
WHERE statusid = 3
AND Dateadd(MONTH, Datediff(MONTH, 0
,
tt1.createddate), 0) =
Dateadd(MONTH, Datediff(MONTH, 0,
tt.createddate), 0)
GROUP BY Dateadd(MONTH, Datediff(MONTH, 0,
createddate), 0)), 0),
( CONVERT(VARCHAR(4), Dateadd(MONTH, Datediff(MONTH, 0, createddate), 0), 100) +
CONVERT(VARCHAR(4), YEAR(Dateadd(MONTH, Datediff(MONTH, 0, createddate), 0)))
) AS prodcountdt
FROM #table_temp tt
WHERE Dateadd(MONTH, Datediff(MONTH, 0, createddate), 0) BETWEEN
Getdate() - 365 AND Getdate()
GROUP BY Dateadd(MONTH, Datediff(MONTH, 0, createddate), 0)
ORDER BY Dateadd(MONTH, Datediff(MONTH, 0, createddate), 0)




Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
EjSQLme
EjSQLme
SSC-Enthusiastic
SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)

Group: General Forum Members
Points: 182 Visits: 431
Okay Thank you guys
Peter Brinkhaus
Peter Brinkhaus
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2064 Visits: 7369
And to simplify things ...


select
'ActiveProd' = COUNT(CASE WHEN StatusID = 1 THEN 1 END),
'InactiveProd' = COUNT(CASE WHEN StatusID = 2 THEN 1 END),
'DiscontinutedProd' = COUNT(CASE WHEN StatusID = 3 THEN 1 END),
(convert(varchar(4),dateadd(month, datediff(month, 0, CreatedDate),0),100) + convert(varchar(4),year(dateadd(month, datediff(month, 0, CreatedDate),0)))) as ProdCountDT
from #Table_Temp TT
where dateadd(month, datediff(month, 0, CreatedDate),0) between getdate() - 365 and getdate()
group by dateadd(month, datediff(month, 0, CreatedDate),0)
order by dateadd(month, datediff(month, 0, CreatedDate),0)


mimi 41746
mimi 41746
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 1
select
'ActiveProd' = COUNT(CASE WHEN StatusID = 1 THEN 1 END),
'InactiveProd' = COUNT(CASE WHEN StatusID = 2 THEN 1 END),
'DiscontinutedProd' = COUNT(CASE WHEN StatusID = 3 THEN 1 END),
(convert(varchar(4),dateadd(month, datediff(month, 0, CreatedDate),0),100) + convert(varchar(4),year(dateadd(month, datediff(month, 0, CreatedDate),0)))) as ProdCountDT
from #Table_Temp TT
where dateadd(month, datediff(month, 0, CreatedDate),0) between getdate() - 365 and getdate()
group by dateadd(month, datediff(month, 0, CreatedDate),0)
order by dateadd(month, datediff(month, 0, CreatedDate),0)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17066 Visits: 19122
To simplify even more:

SELECT
ActiveProd = COUNT( CASE WHEN StatusID = 1 THEN ProdID END),
InactiveProd = COUNT( CASE WHEN StatusID = 2 THEN ProdID END),
DiscontinutedProd = COUNT( CASE WHEN StatusID = 3 THEN ProdID END),
ProdCountDT = RIGHT( CONVERT(varchar(11),DATEADD(MM, DATEDIFF(MM, 0, CreatedDate),0), 106),8)
FROM #Table_Temp TT
--WHERE dateadd(month, datediff(month, 0, CreatedDate),0) BETWEEN getdate() - 365 and getdate()
GROUP BY DATEDIFF(MM, 0, CreatedDate)
ORDER BY ProdCountDT;



Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 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