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
 General SQL Server Forums
 New to SQL Server Programming
 case statement in where clause

Author  Topic 

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-06-25 : 16:53:54
How do i do i case statement in a where clause

something like

where product = 1

case something

and product = 2

I get error near case

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-06-25 : 17:25:37
quote:
Originally posted by CrazyT

How do i do i case statement in a where clause

something like

where product = 1

AND CASE
WHEN Boolean_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
]
END

and product = 2

I get error near case

Go to Top of Page

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-06-25 : 17:31:20
kind of need something like this -- when the length of password is > 0 then add where password = @password to the where statement else dont

this works

and
(Case WHEN LEN(ISNULL(Password,''))>0

then

Password

else

Password

End)= @Password

but doesnt really do what i want

Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-26 : 08:00:34
Why do you need a case statement?It can be easily achieved by using a where clause.Something like this


declare @param as varchar(40)='test'
select * from yourtable where (len(@param)>0 and Password=@param)



Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-06-28 : 11:41:44
Problem is this:

The database has some string.empty cases and some null entires for the password field

so i dont want to even consider the "and passowrd = @password" in the where clause if @password is null.

so thats the reason for the case statement

i guess i can do it with an if/then and have both complete queries in it. didnt know if there was a better way to just not include the "and passowrd = @password"
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-06-28 : 11:51:52

AND PASSWORD = COALESCE(@PASSWORD,PASSWORD)

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -