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


How to write Conditional Where Clause


How to write Conditional Where Clause

Author
Message
descentflower
descentflower
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 Visits: 154
Hi,
I have a simple query

select name from tableInfo

i am passing two parameters to the stored procedure
@x and @y

if @x!=0 and x is not null i want to add that in where clause for exp.
select name from tableInfo where x=@x

Similarly for @y
select name from tableInfo where x=@x and y=@y

I had use dynamic query earlier but i want to avoid it.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)

Group: General Forum Members
Points: 547144 Visits: 47733
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


dilip_baboo
dilip_baboo
SSC-Enthusiastic
SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)

Group: General Forum Members
Points: 103 Visits: 53
try along these lines .....

select * from Tname
where (Tcol1 = @X OR Tcol1 is null)
AND (Tcol2 = @Y OR Tcol2 is null)


---- OR -----


select * from Tname
where Tcol1 = case when @X is null then null
when @X = 0 then null
else @X end
and
Tcol2 = case when @Y is null then null
when @Y = 0 then null
else @Y end
descentflower
descentflower
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 Visits: 154
Hi dilip,

select * from Tname
where Tcol1 = case when @X is null then null
when @X = 0 then null
else @X end
and
Tcol2 = case when @Y is null then null
when @Y = 0 then null
else @Y end

I tried this , but when i pass @x=0 it doesnt works. The Case makes it Tcol1=null.
Pandian S
Pandian S
Old Hand
Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)

Group: General Forum Members
Points: 343 Visits: 143
Hi

SELECT [NAME] FROM TABLEINFO(NOLOCK)
WHERE X = CASE WHEN @X <>0 AND @X IS NOT NULL THEN @X ELSE X END
AND Y = CASE WHEN @Y <>0 AND @Y IS NOT NULL THEN @Y ELSE Y END

Regards

descentflower
descentflower
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 Visits: 154
w00t HI Thank you it worked !! w00t
GilaMonster
GilaMonster
SSC Guru
SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)

Group: General Forum Members
Points: 547144 Visits: 47733
Just a couple things with that.

It's likely to perform badly. Take a look at the blog post I referenced earlier. Also, before you use nolock, read this: http://sqlblog.com/blogs/andrew_kelly/archive/2009/04/10/how-dirty-are-your-reads.aspx

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


descentflower
descentflower
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 Visits: 154
GilaMonster (6/17/2009)
Just a couple things with that.

It's likely to perform badly. Take a look at the blog post I referenced earlier. Also, before you use nolock, read this: http://sqlblog.com/blogs/andrew_kelly/archive/2009/04/10/how-dirty-are-your-reads.aspx


Hi Gila,
As i am a newbie i donno so,
What bad effect will the query with 'case in where clause' bring ?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)SSC Guru (547K reputation)

Group: General Forum Members
Points: 547144 Visits: 47733
Poor performance. Generally that kind of query does not use indexes effectively and often does table scans. On small tables that aren't used too often that won't be a major issue. On larger tables or busy systems it is.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Pyay Nyein
Pyay Nyein
Old Hand
Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)

Group: General Forum Members
Points: 331 Visits: 404
Hi,

Half of the code is already in your question..change the and/or in the where clause according to ur requirement

SELECT [name]
FROM tableInfo
WHERE (@x <> 0 and @x is not null and x = @x)
and/or (@y <> 0 and @y is not null and y = @y)
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