日期:2014-05-18  浏览次数:20579 次

再求一条sql语句,代换字符
假设有下表

ID Description Platform
1 aaa C
2 bbb W
3 ccc C
4 ddd W
5 eee W

要求写出sql语句,呈现这样的结果,凡是
platform是C的,打印出CS-(Description)
是W的,打印出WEB-(Description)
结果基本上是这样

CS-aaa
WEB-bbb
CS-ccc
WEB-ddd
WEB-eee

该怎样写呢?谢谢 


------解决方案--------------------
SQL code
select case when Platform='C' then 'CS-'+Description
            when Platform='W' then 'Web-'+Description end
from tb

------解决方案--------------------
SQL code

declare @t table (ID int,Description varchar(3),Platform varchar(1))
insert into @t
select 1,'aaa','C' union all
select 2,'bbb','W' union all
select 3,'ccc','C' union all
select 4,'ddd','W' union all
select 5,'eee','W'

select ID,C1+Description as Description from @t a 
left join (select 'CS-' AS C1 UNION SELECT 'WEB-') b on a.Platform=left(b.C1,1)
/*
ID          Description
----------- -----------
1           CS-aaa
2           WEB-bbb
3           CS-ccc
4           WEB-ddd
5           WEB-eee
*/