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 2005 Forums
 Transact-SQL (2005)
 Concatenating using the IIF statement

Author  Topic 

Trudye
Posting Yak Master

169 Posts

Posted - 2008-04-16 : 18:20:42
Hi Guys,

I am converting Access queries to SQL. One query has a string of IIF statements that can concatenate several strings into one field. For instance:

IIf([Hot Dogs]=-1,"* Wants a Hot Dog ","") &
IIf([Hamburger]=-1,"*Wants a Hamburger ","") &
IIf([mustard]=-1,"*add mustard ","") &
IIf([fries]=-1,"*Include Fries ","") &
IIf([Chips]=-1,"*Include Chips ","")

Consequently the output could be:
*Hamburger *add mustard *Include Chips


I know I can use the ‘Case When Then’ statement but how do I concatenate?

Thanx

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-16 : 18:33:48
In T-SQL, you can concatenate with +.

SELECT 'Tara' + ' Kizer'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-04-16 : 19:56:36
see:
http://weblogs.sqlteam.com/jeffs/archive/2007/03/30/Quick-Access-JET-SQL-to-T-SQL-Cheatsheet.aspx

by the way, that made me really hungry .....

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

Trudye
Posting Yak Master

169 Posts

Posted - 2008-04-17 : 09:31:39
Guys, thanks so much for the feedback. I know I have to replace the IIF with a CASE statement what I can't figure out is how to concatenate the mulitple strings that can occur

*Hamburger *add mustard *Include Chips
comes from 3 different When statements, where to I place the '+' sign.

Thanx,
Trudye
Go to Top of Page

Trudye
Posting Yak Master

169 Posts

Posted - 2008-04-17 : 11:29:14
Thanx so much guys for all your input, I finally figures it out.

case when[hot dogs] = -1 then '* Wants a Hot Dog ' else '' end +
case when[Hamburger]=-1 then '*Wants a Hamburger ' else '' end +
case when[mustard]=-1 then '*add mustard ' else '' end +
case when[fries]=-1 then '*Include Fries ' else '' end +
case when[Chips]=-1 then '*Include Chips ' else '' end

Thanx again
Go to Top of Page
   

- Advertisement -