在Oracle中有如下一段SQL:
select case to_char(sysdate, 'dd')
when '01' then exp_1
when '02' then exp_2
when '03' then exp_3
...
else exp_n
end
from table_1
如何将这段SQL改写成符合SQL-99标准?
我试着使用CAST,但不知如何处理格式'dd'.
...全文
2798打赏收藏
如何替换to_char(<date>, <format>), 使它成为标准SQL
在Oracle中有如下一段SQL: select case to_char(sysdate, 'dd') when '01' then exp_1 when '02' then exp_2 when '03' then exp_3 ... else exp_n end from table_1 如何将这段SQL改写成符合SQL-99标准? 我试着使用CAST,但不知如何处理格式'dd'.
to_char()是Oracle独有的,不属于标准SQL吧! 换句话说,我想上面的SQL在所有遵循SQL-99的database上都能运行,如DB2,SQLServer...都能得出正确结果.
如果能将掩码加入到CAST函数中,我的问题就解决.但我查了些资料文档,没有发现在标准SQL实现类似功能:
cast('12-OCT-2003 as date, format 'dd-mon-yyyy'), --指定格式String转成日期
cast(sysdate as varchar2(2) format 'dd) --指定日期转成特定格式字符
cast 是可以进行数据类型转换,但我不知如何加入日期类型的格式.如'YYYY', 'MM', 'DD' 之类.
select case cast(sysdate as date) -- 所得的是字符串'11-OCT-2003', 但我想行到'11'
when '01' then exp_1
when '02' then exp_2
when '03' then exp_3
...
else exp_n
end
from table_1