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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 condition in having clause

Author  Topic 

magmo
Aged Yak Warrior

558 Posts

Posted - 2012-10-30 : 04:33:49
Hi

I have the following part of a query....



HAVING ';'+@CompanyIDs+';' LIKE '%;'+CAST(dbo.SurveyAnswerInfo.CompanyID AS VARCHAR(32))+';%' AND (dbo.SurveyAnswerInfo.DateAdded >= @FromDate) AND (dbo.SurveyAnswerInfo.DateAdded < @ToDate) AND (dbo.Users.GenderID = @GenderID)



I would like to filter based on GenderID if that value is greater than 0, otherwise there should be no filtering at all based on GenderID, how would I do that
?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-10-30 : 07:14:35
Change your filter for GenderId to this:
       AND (dbo.Users.GenderID = @GenderID OR @GenderID = 0) 
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-10-30 : 07:47:28
Why do you have this in a having clause rather than a where clause?

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

magmo
Aged Yak Warrior

558 Posts

Posted - 2012-10-30 : 11:25:29
If the GenderID is equal to 0 then that means that I dont want to do a search based on GenderID at all, there is no GenderID equal to 0
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-30 : 11:29:02
quote:
Originally posted by magmo

If the GenderID is equal to 0 then that means that I dont want to do a search based on GenderID at all, there is no GenderID equal to 0


thats exactly what Sunita's suggestion does

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

magmo
Aged Yak Warrior

558 Posts

Posted - 2012-10-30 : 11:36:29
No its not, that give me all rows that either contains a certain GenderID or GenderID that is equal to 0.

There are 2 possible GenderID, 1 or 2 if I pass on GenderID = 0 to my stored procedure I want to select both GenderID 1 and 2.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-30 : 11:38:11
quote:
Originally posted by magmo

No its not, that give me all rows that either contains a certain GenderID or GenderID that is equal to 0.

There are 2 possible GenderID, 1 or 2 if I pass on GenderID = 0 to my stored procedure I want to select both GenderID 1 and 2.


nope
notice the @gender=0 part its referring to gender parameter and not gender field in the table

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

magmo
Aged Yak Warrior

558 Posts

Posted - 2012-10-30 : 11:48:46
Your absolutly right, I stand corrected. Thanks a bunch!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-30 : 12:13:35
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -