all groups > sql server programming > november 2004 >
You're in the

sql server programming

group:

SQL udp help needed


SQL udp help needed Mike Hoff
11/2/2004 10:40:36 PM
sql server programming:
I am trying to make a query that returns different strings added to a field,
depending on the value of another field. The following pseudo-sql
demonstrates what I would LIKE to do. I hope this makes sense. Table has 3
fields: RecID - int, RecCode - int, RecName - varchar.

SELECT RecID, [IF (RecCode=1 then 'one'+RecName) ELSE IF (RecCode=2 then
'two'+RecName) ELSE ('neither'+RecName)] as MyName

Any help appreciated.

Re: SQL udp help needed Dan Guzman
11/2/2004 10:51:36 PM
You can use CASE like in the example below. See the Books Online for
details.

SELECT
RecID,
CASE RecCode
WHEN 1 THEN 'one'+RecName
WHEN 2 THEN 'two'+RecName
ELSE 'neither'+RecName
END AS MyName
FROM MyTable

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]

Re: SQL udp help needed Mike Hoff
11/3/2004 12:30:41 AM
Dan,

This worked perfectly. I kept looking for an Immediate IF solution - didnt
think of a CASE - thanks a lot!

[quoted text, click to view]

AddThis Social Bookmark Button