如果它是字符串常量,如何使用“ sysdate”

时间:2019-06-24 10:01:03

标签: sql oracle oracle-apex

我从表中提取数据,该字段通常为null,但有时是sysdate。但是,由于它是从表中获取的,因此在单引号之间获取字段get_quotes之后。我该怎么用?

我尝试了to_date,to_date(to_char())。 我需要

'sysdate'

2 个答案:

答案 0 :(得分:3)

您可以使用大小写表达式:

select case 
          when the_column = 'sysdate' then sysdate
          else to_date(the_column)
       end as date_value
from the_table;

答案 1 :(得分:2)

我知道的唯一方法是动态SQL 。这是一个示例:

SQL> create table test (id number, col varchar2(20));

Table created.

SQL> insert into test
  2    select 1, '''sysdate''' from dual union all
  3    select 2, null          from dual;

2 rows created.

SQL> declare
  2    l_res test%rowtype;
  3    l_str varchar2(200);
  4  begin
  5    for cur_r in (select id, col from test) loop
  6      l_str := 'select ' || cur_r.id ||', '||
  7                  nvl(replace(cur_r.col, chr(39), null), 'null') || ' from dual';
  8      execute immediate l_str into l_res;
  9      dbms_output.put_line(l_res.id ||': '|| l_res.col);
 10    end loop;
 11  end;
 12  /
1: 24.06.2019 12:18:39
2:

PL/SQL procedure successfully completed.

SQL>