-1

So if I got a column like this:

Notes   
A, B, C    
A    
A, C    
A, F

How do i do a multiple if statements that would look through the column and if it has an A print "cash", if it has a B print "Check", if it has a C print "money order". And concatenate them into one string. Or if a case statement would be better?

Hart CO
  • 34,064
  • 6
  • 48
  • 63
jjohnson
  • 203
  • 1
  • 5
  • 16
  • How many distinct values are you checking for, and are they at all likely to change? And what database are you using? – Hart CO Dec 12 '14 at 16:55
  • And is changing the design an option? CSV in a column is seldom a good idea, probably never if you want to process individual items in SQL. – Andriy M Dec 12 '14 at 17:12
  • There are 7 distinct values and no they are not likely to change. I'm using SQL server 2008. – jjohnson Dec 16 '14 at 19:34

2 Answers2

1
select 
case when Notes like '%A%' then 'cash' else '' end + 
case when Notes like '%B%' then 'Check' else '' end + 
case when Notes like '%C%' then 'money order' else '' end as 
customstring
from table
overflowed
  • 1,773
  • 10
  • 13
  • How would I go about putting "or" before the last option and commas after each option but not after the last option? Thanks in advance. – jjohnson Dec 16 '14 at 18:13
0
select replace(replace(replace(Notes,'A','cash'),'C','money order'),'B','Check')

or you can use Case statement also

void
  • 7,760
  • 3
  • 25
  • 43
  • How would I go about putting "or" before the last option and commas after each option but not after the last option? Thanks in advance. – jjohnson Dec 17 '14 at 16:53