您可以使用當前查詢作爲派生表:
select YourString,
LEN(YourString) [Length],
Case
When LEN(YourString) > 35 THEN REPLACE(YourString,testname,'')
Else YourString
End YourNewString
from ( select 'Test '+m.testname+' '+
Case
When m.value = 'ttttt' Then 'tt'
When m.styleName = 'ppppp' Then 'pp'
When m.styleName = 'qqqqq' Then 'qq'
When m.styleName = 'yyyyyy' Then 'yy'
Else ''
End YourString,
m.testname
from testtable m
where m.id=10) a
;
或者你可以使用一個CTE:
WITH CTE AS
(
select 'Test '+m.testname+' '+
Case
When m.value = 'ttttt' Then 'tt'
When m.styleName = 'ppppp' Then 'pp'
When m.styleName = 'qqqqq' Then 'qq'
When m.styleName = 'yyyyyy' Then 'yy'
Else ''
End YourString,
m.testname
from testtable m
where m.id=10
)
select YourString,
LEN(YourString) [Length],
Case
When LEN(YourString) > 35 THEN REPLACE(YourString,testname,'')
Else YourString
End YourNewString
from CTE;
thanksss其工作 – isree