May 10, 2012 at 4:05 pm
this query return null when no data found why?
select
SUM([TedadTarhModiriatTafzili])as TedadTarhModiriatTafzili
, SUM([TedadTarhModiriatJame])as TedadTarhModiriatJame
, SUM([TedadKhodro])as TedadKhodro
, SUM([TedadMotorcycle])as TedadMotorcycle
, SUM([TedadGhayegh])as TedadGhayegh
, SUM([TedadBisim])as TedadBisim
, SUM([TedadDoorbinAksbardari])as TedadDoorbinAksbardari
, SUM([TedadDoorbinCheshmi])as TedadDoorbinCheshmi
FROM [MantagheInfo]
WHERE (YearID = @year)
May 10, 2012 at 6:55 pm
Because tyou can't do much with null.
What do you want it to return? 0? If so, wrap each SUM in an ISNULL.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 10, 2012 at 11:47 pm
i want when no data is available (on where statement no data is exist) return nothing not null value
select
SUM(isnull([TedadTarhModiriatTafzili],0))as TedadTarhModiriatTafzili
, SUM(isnull([TedadTarhModiriatJame],0))as TedadTarhModiriatJame
, SUM(isnull([TedadKhodro],0))as TedadKhodro
, SUM(isnull([TedadMotorcycle],0))as TedadMotorcycle
, SUM(isnull([TedadGhayegh],0))as TedadGhayegh
, SUM(isnull([TedadBisim],0))as TedadBisim
, SUM(isnull([TedadDoorbinAksbardari],0))as TedadDoorbinAksbardari
, SUM(isnull([TedadDoorbinCheshmi],0))as TedadDoorbinCheshmi
FROM [MantagheInfo]
WHERE (YearID = @year)
May 10, 2012 at 11:55 pm
Your code should be changed like below
SELECT ISNULL(SUM([TedadTarhModiriatTafzili]), 0)AS TedadTarhModiriatTafzili ,
ISNULL(SUM([TedadTarhModiriatJame]), 0)AS TedadTarhModiriatJame ,
ISNULL(SUM([TedadKhodro]), 0)AS TedadKhodro ,
ISNULL(SUM([TedadMotorcycle]), 0)AS TedadMotorcycle ,
ISNULL(SUM([TedadGhayegh]), 0)AS TedadGhayegh ,
ISNULL(SUM([TedadBisim]), 0)AS TedadBisim ,
ISNULL(SUM([TedadDoorbinAksbardari]), 0)AS TedadDoorbinAksbardari ,
ISNULL(SUM([TedadDoorbinCheshmi]), 0)AS TedadDoorbinCheshmi
FROM [MantagheInfo]
WHERE ( YearID = @year )
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
May 11, 2012 at 8:42 am
Like this?
if exists
(
select 1
FROM [MantagheInfo]
WHERE (YearID = @year)
)
select
SUM(isnull([TedadTarhModiriatTafzili],0))as TedadTarhModiriatTafzili
, SUM(isnull([TedadTarhModiriatJame],0))as TedadTarhModiriatJame
, SUM(isnull([TedadKhodro],0))as TedadKhodro
, SUM(isnull([TedadMotorcycle],0))as TedadMotorcycle
, SUM(isnull([TedadGhayegh],0))as TedadGhayegh
, SUM(isnull([TedadBisim],0))as TedadBisim
, SUM(isnull([TedadDoorbinAksbardari],0))as TedadDoorbinAksbardari
, SUM(isnull([TedadDoorbinCheshmi],0))as TedadDoorbinCheshmi
FROM [MantagheInfo]
WHERE (YearID = @year)
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 11, 2012 at 8:53 am
vahid.arr (5/10/2012)
i want when no data is available (on where statement no data is exist) return nothing not null value
select
SUM(isnull([TedadTarhModiriatTafzili],0))as TedadTarhModiriatTafzili
, SUM(isnull([TedadTarhModiriatJame],0))as TedadTarhModiriatJame
, SUM(isnull([TedadKhodro],0))as TedadKhodro
, SUM(isnull([TedadMotorcycle],0))as TedadMotorcycle
, SUM(isnull([TedadGhayegh],0))as TedadGhayegh
, SUM(isnull([TedadBisim],0))as TedadBisim
, SUM(isnull([TedadDoorbinAksbardari],0))as TedadDoorbinAksbardari
, SUM(isnull([TedadDoorbinCheshmi],0))as TedadDoorbinCheshmi
FROM [MantagheInfo]
WHERE (YearID = @year)
By "Nothing" do you mean blank or 0?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 11, 2012 at 8:57 am
I think the OP wants no result set at all when there is no row. 0 row(s) returned.
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply