如何有条件地选择 Oracle 查询中的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15665220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to conditionally select a column in an Oracle query
提问by Travis
I want to do something like:
我想做类似的事情:
select (if lookup = 8 then 08 else lookup) lookup
, //more columns
from lookup_table
order by lookup
Unfortunately, Oracle doesn't seem to like this syntax, and I am failing to discover why or find what other function I should be using.
不幸的是,Oracle 似乎不喜欢这种语法,而且我没有发现原因或找到我应该使用的其他函数。
Basically, if the lookup value is 8, I want to get 08, otherwise I want the value of lookup. I'm sure I'm just being stupid.
基本上,如果lookup值为8,我想得到08,否则我想要lookup的值。我确定我只是愚蠢。
回答by Gordon Linoff
You want a case statement:
你想要一个 case 语句:
select (case when lookup = 8 then 8 else lookup end) as lookup
If lookup
is a character string, you probably want:
如果lookup
是字符串,您可能想要:
select (case when lookup = '08' then '08' else lookup end) as lookup
If lookup
is an integer and you want to convert it to a string, then:
如果lookup
是整数并且您想将其转换为字符串,则:
select (case when lookup = 8 then to_char(lookup, '00') else to_char(lookup, '00') end) as lookup
However, that would seem redundant to me.
然而,这对我来说似乎是多余的。