Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL - No return value, but I need one 1

Status
Not open for further replies.

zemp

Programmer
Joined
Jan 27, 2002
Messages
3,301
Location
CA
SQL Server 2000 SP3

I have the following T-SQL that does not return anything. Which is correct because no records match the criteria (a possible case that must be handled). However I need to be able to return a zero (0) in this case so that the return value can be used in a math function as part of a larger stored procedure. I am at a loss. Any suggestions?
Code:
SELECT  SUM(CDHours) as hours
FROM         TCDetail
WHERE     (CDDate <= '07/29/2005') 
GROUP BY ECode
HAVING      (ECode = 'STA')

zemp
 
will this work?

Code:
If Exists(Select 1 From TCDetail Where ECode='STA'
  SELECT  SUM(CDHours) as hours
  FROM         TCDetail
  WHERE     (CDDate <= '07/29/2005') 
  GROUP BY ECode
  HAVING      (ECode = 'STA')
Else
  Select 0 As Hours

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Ooops, obvious errors.

Code:
If Exists(Select 1 From TCDetail Where ECode='STA' And CDDate <= '07/29/2005')
  SELECT  SUM(CDHours) as hours
  FROM         TCDetail
  WHERE     (CDDate <= '07/29/2005') 
  GROUP BY ECode
  HAVING      (ECode = 'STA')
Else
  Select 0 As Hours

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks George, That will work fine!

zemp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top