Groups | Blog | Home
all groups > sql server programming > january 2006 >

sql server programming : Case Statement



--CELKO--
1/31/2006 2:16:21 PM
[quoted text, click to view]
working but when I add in a IN... to the case statement it doesn't
return
anything. <<

There is no CASE statement in SQL; there is a CASE expression! You are
also confusing columns and fields.

SELECT *
FROM Sometable
WHERE some_col
= (CASE @variable
WHEN 1 THEN 'sometext1'
WHEN 2 THEN 'sometext2'
WHEN 3 THEN 'sometext1, sometext2'
ELSE NULL END);
Mark Williams
1/31/2006 2:21:29 PM
SQL is interpreting 'sometext1, sometext2' as a single string, and not
expanding it to a set the way you would like it to.

Try this instead:

DECLARE @Variable int

Select * from dbo.Sometable
where
CASE
WHEN SomeTable.SomeField = 'sometext1' THEN 1
WHEN SomeTable.SomeField = 'sometext1' THEN 2
WHEN SomeTable.SomeField IN ('sometext1','sometext2') THEN 3
ELSE 0
END = @Variable


--
[quoted text, click to view]
Mark Williams
1/31/2006 2:25:20 PM
Oops. What I meant was

DECLARE @Variable int

Select * from dbo.Sometable
where
CASE
WHEN SomeTable.SomeField = 'sometext1' THEN 1
WHEN SomeTable.SomeField = 'sometext2' THEN 2 --correction made here
WHEN SomeTable.SomeField IN ('sometext1','sometext2') THEN 3
ELSE 0
END = @Variable

--

[quoted text, click to view]
Richard Thayne
1/31/2006 9:53:43 PM
I am trying to create a statement that has a case in the where. I have it
working but when I add in a IN... to the case statement it doesn't return
anything.

Sample.

Select * from dbo.Sometable
Where SomeTable.SomeField IN (case @Variable when 1 then 'sometext1' When
2 then 'sometext2' when 3 then 'sometext1, sometext2')

passing 1 works
passing 2 works
passing 3 nothing returns

Any Ideas?

Thanks

Richard Thayne
1/31/2006 10:53:13 PM
Hello --CELKO--,

This did not work.

[quoted text, click to view]

AddThis Social Bookmark Button