简体   繁体   English

SQL ORACLE SELECT案例和NVL

[英]SQL ORACLE SELECT CASE and NVL

I would like to ask how to use NVL and CASE in one Statement. 我想问一下如何在一个Statement中使用NVL和CASE。

SELECT case 
          when column1 like '% =' then replace(column1,' =','=') 
          else column1 
       end 
from table;

SELECT nvl(SUBSTR(column1, INSTR(column1, 'text1') +3, 
           INSTR(column1, 'text2')-(INSTR(column1, ':text1')+4)),'Default') 
from table;

What I want is to use the output of select statement in case as the column1 in nvl statement. 我想要的是将select语句的输出用作nvl语句中的column1。

Maybe you could use a sub select: 也许您可以使用子选择:

SELECT nvl(SUBSTR(column2, INSTR(column2, 'text1') +3, 
           INSTR(column2, 'text2')-(INSTR(column2, ':text1')+4)),'Default') 
from 
(
SELECT case 
          when column1 like '% =' then replace(column1,' =','=') 
          else column1 
       end column2
from test);

Unless you are worried about strings with equal signs not appearing at the end of the string, I wouldn't bother with the case statement, and if you are concerned about that, I'd recommend the REGEXP_REPLACE function instead. 除非您担心带有等号的字符串不会出现在字符串的末尾,否则我不会理会case语句,并且如果您对此感到担心,我建议您使用REGEXP_REPLACE函数。 The following function simply removes a single space preceding the terminating equals sign in the string stored in column1: 以下函数仅删除存储在column1中的字符串中的终止等号前面的单个空格:

REGEXP_REPLACE(column1,' =$','=')

Then put the above function anywhere column1 appears in your select statement. 然后将上述函数放在select语句中column1出现的任何位置。

select COALESCE( SUBSTR( REGEXP_REPLACE(column1,' =$','=')
                       , INSTR(REGEXP_REPLACE(column1,' =$','='), 'text1')+3
                       , INSTR(REGEXP_REPLACE(column1,' =$','='), 'text2')
                       -(INSTR(REGEXP_REPLACE(column1,' =$','='), ':text1')+4))
               , 'Default')
 from table;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM