SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Determine type by variable
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ugh3012
Yak Posting Veteran

62 Posts

Posted - 09/25/2013 :  15:40:46  Show Profile  Reply with Quote
I have a situation where I need to determine which type this record is. If type ID is 20, 23, or 25, then select F otherwise it is P. Here is a sample query to help explain my question.

Select FirstName
,Lastname
,address
, (If typeid = 20, 23, or 25 then F else P) as TypeDesc
From sometable


The number(20,23,25) are hardcoded in the stored procedure and I have them in a variable becasue there are over 30 diffwerent numbers.

James K
Flowing Fount of Yak Knowledge

3587 Posts

Posted - 09/25/2013 :  15:45:42  Show Profile  Reply with Quote
Use case expression rather than IF
Select FirstName
,Lastname
,address
, CASE WHEN typeid IN ( 20, 23,  25) then F else P END as TypeDesc
From sometable
Are F and P columns in sometable? If they are not, i.e., if they are literals, then use 'F' and 'P' with single quotes.

If the list of type id's is in a variable, how is it stored? In a varchar variable with comma separating individual type ids, or perhaps in some other manner?
Go to Top of Page

ugh3012
Yak Posting Veteran

62 Posts

Posted - 09/25/2013 :  16:25:16  Show Profile  Reply with Quote
This is what I have for ( 20, 23, 25)

Delcare @var varchar(20)
Set @var = '(20, 23, 25)'
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3587 Posts

Posted - 09/25/2013 :  17:14:08  Show Profile  Reply with Quote
This:
Select FirstName
,Lastname
,address
, CASE WHEN ','+REPLACE(REPLACE(REPLACE(@var,')',''),'(',''),' ','')+','
	LIKE '%,'+CAST(typeid AS VARCHAR(20)) + ',%' THEN F ELSE P END AS TypeDesc
From sometable
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000