如何在Oracle查询中有条件地选择列 [英] How to conditionally select a column in an Oracle query

查看:72
本文介绍了如何在Oracle查询中有条件地选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做类似的事情:

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

不幸的是,Oracle似乎不喜欢这种语法,而且我无法找到原因或找到我应该使用的其他功能.

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.

基本上,如果查找值为8,我想得到08,否则,我想要查找的值.我确定我只是愚蠢.

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.

推荐答案

您需要个案说明:

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

如果lookup是字符串,则可能需要:

If lookup is a character string, you probably want:

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

如果lookup是整数,并且您想将其转换为字符串,则:

If lookup is an integer and you want to convert it to a string, then:

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.

这篇关于如何在Oracle查询中有条件地选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆