Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear,

I have a table called Dummy, On that column are Dm_Ex, Dm_SL, Dm_Ad all data type numeric.
What I want if Dm_Ex>0 then ‘Ex’ if Dm_SL>0 then ‘SL’ if Dm_Ad>0 then ‘AD’

HTML
Dm_Ex	Dm_SL	Dm_Ad	Result Want
1	0	1	EX,AD
1	1	1	EX,SL,AD
0	1	1	SL,AD
1	0	0	EX


I used case but not success.
SQL
CASE WHEN Dm_Ex > 0 THEN 'EX' WHEN Dm_SL > 0 THEN 'SL' WHEN Dm_Ad > 0 THEN 'AD' ELSE 'N/A' END


Thanks
Basit.
Posted

1 solution

If the values are always zero or one, then I'd cheat, a bit.
I'd create a Mapping table:
VB
Value   Result
0       N/A
1       AD
2       SL
3       SL,AD
4       EX
5       EX,AD
6       EX,SL
7       EX,SL,AD

And just use a trivial JOIN:
SQL
SELECT d.*, m.Result FROM Dummy d
JOIN MapText m
ON (d.Dm_Ex * 4) + (d.Dm_SL * 2) + d.Dm_Ad = m.Value
 
Share this answer
 
v2
Comments
[no name] 17-Sep-15 6:26am    
Maybe a little bit "cheated", but very readable and understandable. +5.
jaket-cp 23-Sep-15 4:13am    
nice :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900