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 2012 Forums
 Transact-SQL (2012)
 case statement under where condition

Author  Topic 

kirank
Yak Posting Veteran

58 Posts

Posted - 2014-06-06 : 17:18:36
Hi,

Just help me with below syntax, wanted to add condition on where clause that.

SELECT *
FROM RelationTbl WITH (NOLOCK)
WHERE
CASE WHEN Len(@variableName) = 14
THEN
// this condition should work in where clause
ELSE
// another condition
END

I'm not sure how i can put the condition in then & else. basically will put value and base on case then or else get executed.

Here i want to check if Len(@variableName) = 14 then
REF = @variableName // this query execute
ELSE
REF = @variableName and NumType is null // this query execute

Thanks,



---------------------------

http://codingstuffsbykiran.blogspot.com | http://webdevlopementhelp.blogspot.com

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-06-06 : 17:22:16
WHERE (LEN(@variableName)=14 AND REF=@variableName) OR (REF=@variableName AND NumType IS NULL)

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

kirank
Yak Posting Veteran

58 Posts

Posted - 2014-06-06 : 17:30:39
Oh it's too simple, thanks for pointing!

---------------------------

http://codingstuffsbykiran.blogspot.com | http://webdevlopementhelp.blogspot.com
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-06-06 : 17:54:00


Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -