且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQL Server 2005中解码

更新时间:2023-09-23 10:56:04

da***@yahoo.com 写道:
da***@yahoo.com wrote:

我是SQL Server的新手,所以这可能是一个非常简单的问题

来修复。我正在翻译Oracle查询,需要将

''解码'更改为兼容的东西。我读过的所有内容都指向我使用''case''
但是无论我怎么写它都不能让它工作并且

得到一个语法错误。建议?


选择SYST CTR

,isnull(substring(CD_A,1,3),'''')RESCODE

,DES描述

,解码(子串(CD_A,1,3),CODE,PRICE,0)UNIT_PRICE

,count(distinct OR_NO)QTYW
I am a novice to SQL Server, so this is probably a really easy problem
to fix. I''m translating an Oracle query and need to change the
''decode'' to something compatible. Everything I''ve read points me to
using ''case'' but no matter how I write it I can''t get it to work and
get a syntax error. Suggestions?

select SYST CTR
, isnull(substring(CD_A, 1, 3), '' '') RESCODE
, DES DESCRIPTION
, decode (substring(CD_A, 1, 3), CODE,PRICE,0) UNIT_PRICE
, count (distinct OR_NO) QTYW



* googles(Oracle解码函数)*


选择SYST CTR

,isnull(substring (CD_A,1,3),'''')RESCODE

,DES描述

,案例子串(CD_A,1,3)

当CODE然后PRICE

否则0

结束UNIT_PRICE

,count(distinct OR_NO)QTYW

*googles (Oracle "decode function")*

select SYST CTR
, isnull(substring(CD_A, 1, 3), '' '') RESCODE
, DES DESCRIPTION
, case substring(CD_A, 1, 3)
when CODE then PRICE
else 0
end UNIT_PRICE
, count (distinct OR_NO) QTYW


选择SYST CTR
select SYST CTR

,isnull(substring(CD_A,1,3),''')RESCODE

,DES描述

,案例子串(CD_A,1,3)

*当CODE然后价格

*其他0

结束UNIT_PRICE

,count(不同的OR_NO)QTYW
, isnull(substring(CD_A, 1, 3), '' '') RESCODE
, DES DESCRIPTION
, case substring(CD_A, 1, 3)
* when CODE then PRICE
* else 0
end UNIT_PRICE
, count (distinct OR_NO) QTYW



谢谢。效果很好!

Thank you. That worked great!


Oracle现在也有CASE表达式。
Oracle now has the CASE expression, too.