开发者

Built in Decode Function in SQL Server 2008

I am migrating my db from oracle to SQL Server 2008.Can you show a built in function that can work same as oracle's decode? I don't want to wr开发者_开发百科ite case when in every query.


There is no equivalent function in T-SQL, so you will have to use CASE.

See here and here.


Apart from using a CASE statement or write an UDF, I think you are out of luck.

According to this link, DECODE is becoming somewhat irrelevant in favor off CASE on Oracle to.

But back to the original topic ...
CASE was quickly adopted, and the
reason is that it provided new and
advantageous functionality over the
older decode.

-- Daniel A. Morgan
Oracle Ace Director & Instructor University of
Washington damorgan@x.washington.edu


You can use CASE:

Select f1, case f2
                         When 1 then 'A'
                         When 2 then 'B'
                         When 3 then 'C'
                         Else 'Some other'
            End as f2_decode
from MyTable
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜