Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CASE in Stored Proc

Status
Not open for further replies.

southbean

Technical User
Jun 3, 2005
147
US
Hello All,
I’ve been hacking away at a stored procedure for a while with no success. Time to ask the experts.
I have a form that uses this query to pull two values from a string. User chooses one value.
Code:
SELECT DISTINCT
	Field =
	CASE 
		WHEN SUBSTRING(Field, 1, 2) IN 
			('A1', 'B1','C1','D1','E1','F1','G1') THEN 'Value1'
		ELSE 'Value2'
	END
FROM
	Table1

I have a variable called @Var1. I’m trying to use a CASE statement in the WHERE clause of my stored proc to

Code:
            CASE  
		WHEN @Var1 = 'Value1’ THEN 
			SUBSTRING(Table. Field, 1, 2) IN ('A1','B1','C1','D1','E1','F1','G1')
		ELSE 
			SUBSTRING(Table. Field, 1, 2) IN ('H1','I1','J1','K1','L1','M1','N1')
            END

It’s not working. Any comments/suggestions would be greatly appreciated!

- tl
 
Code:
WHERE
(@Var1 = 'Value1' AND SUBSTRING(TableName.Field, 1, 2) IN ('A1','B1','C1','D1','E1','F1','G1'))

OR

(ISNULL(@Var1, '') <> 'Value1' AND SUBSTRING(TableName.Field, 1, 2) IN ('A1','B1','C1','D1','E1','F1','G1'))
 
Hello RiverGuy,

Thank you for your reply!

I now get data returned! Excellent!

However, I only get data for 'Value1', even if I select 'Value2'. Any thoughts?

Again, thank you very much for your reply!

- tl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top