【问题标题】:How to conditionally select a column in an Oracle query如何有条件地选择 Oracle 查询中的列
【发布时间】:2013-03-17 21:50:30
【问题描述】:

我想做这样的事情:

select (if lookup = 8 then 08 else lookup) lookup
  , //more columns
from lookup_table
order by lookup

不幸的是,Oracle 似乎不喜欢这种语法,我找不到原因,也找不到我应该使用的其他函数。

基本上,如果lookup值为8,我想得到08,否则我想要lookup的值。我敢肯定我只是愚蠢。

【问题讨论】:

    标签: sql oracle select


    【解决方案1】:

    你想要一个案例陈述:

    select (case when lookup = 8 then 8 else lookup end) as lookup
    

    如果lookup 是一个字符串,你可能想要:

    select (case when lookup = '08' then '08' else lookup end) as lookup
    

    如果lookup 是一个整数并且你想将它转换成一个字符串,那么:

    select (case when lookup = 8 then to_char(lookup, '00') else to_char(lookup, '00') end) as lookup
    

    但是,这对我来说似乎是多余的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多