如何将数字替换/转换为字符串

时间:2018-12-11 08:55:56

标签: sql oracle

我有一个运行良好的查询,并返回-1

SELECT -1 FROM (select SUM(column1), column2 
from documents where column2 is NULL group by column2) aa
where aa.column2_rid = rid and typ_doc in (4,2) and rownum = 1

但是我想将-1转换/替换为字符串-> 'no_doc'

如果我使用replace,它将返回0

我认为,因为这是一个数字.....

NVL不起作用。 To_char(-1)也不起作用。否则我失败了。

SELECT REPLACE('-1', '-1', 'no_doc') FROM ....
output: 0
SELECT REPLACE(-1, -1, 2) FROM .... 
output: 2

如何实现预期的输出? ('no_doc'

3 个答案:

答案 0 :(得分:2)

这将起作用:

select decode((select -1 from dual),-1,'no_doc','anything') from dual;

用于查询:

 select decode((SELECT -1 FROM (select SUM(column1), column2 
 from documents where column2 is NULL group by column2) aa
 where aa.column2_rid = rid and typ_doc in (4,2) and rownum = 
 1),-1,'no_doc','anything') from dual;

答案 1 :(得分:2)

为什么不能在查询中仅使用'no_doc'而不是-1

select 'no_doc'
from (select SUM(column1), column2 
      from documents
      where column2 is NULL
      group by column2
     ) aa
where aa.column2_rid = rid and typ_doc in (4,2) and
      rownum = 1;

答案 2 :(得分:1)

  

您也可以使用NVL2功能

SELECT NVL2(TO_CHAR(-1),'no_doc','Anyhing') FROM (
select SUM(column1), column2 from documents where column2 is NULL group by column2) aa
where aa.column2_rid = rid and typ_doc in (4,2) and rownum = 1