我将所有日期作为varchar2(10)插入表格并格式化为'mm / dd / yyyy'。我需要的是以下格式'mm-dd-yyyy'和日期数据类型。
我没有PLSQL的实现是:
select day||'-'||month||'-'||year as formatted_date from
(select
extract( day from (select to_date('1/21/2000','mm/dd/yyyy') from dual)) as day,
to_number(extract( month from (select to_date('1/21/2000','mm/dd/yyyy') from dual)),09) as month,
extract( year from (select to_date('1/21/2000','mm/dd/yyyy') from dual)) as year
from dual);
select day||'-'||month||'-'||year as formatted_date from
(select
extract( day from (select to_date('1/21/2000','mm/dd/yyyy') from dual)) as day,
to_number(extract( month from (select to_date('1/21/2000','mm/dd/yyyy') from dual)),09) as month,
extract( year from (select to_date('1/21/2000','mm/dd/yyyy') from dual)) as year
from dual);
结果是:21-1-2000不是21-01-2000如预期的那样。
将额外的to_date(,)添加为:
它甚至不会互相改变日期和月份字段。
答案 0 :(得分:17)
您无需提取部分日期。只需使用to_date及其存储的格式将其转换为日期,然后以您想要的格式将该日期转换为char。像这样:
select to_char(to_date('1/10/2011','mm/dd/yyyy'),'mm-dd-yyyy') from dual
答案 1 :(得分:8)
我建议您在转换为字符串时使用TO_CHAR()
。为此,您需要先建立一个日期。
SELECT TO_CHAR(TO_DATE(DAY||'-'||MONTH||'-'||YEAR, 'dd-mm-yyyy'), 'dd-mm-yyyy') AS FORMATTED_DATE
FROM
(SELECT EXTRACT( DAY FROM
(SELECT TO_DATE('1/21/2000', 'mm/dd/yyyy')
FROM DUAL
)) AS DAY, TO_NUMBER(EXTRACT( MONTH FROM
(SELECT TO_DATE('1/21/2000', 'mm/dd/yyyy') FROM DUAL
)), 09) AS MONTH, EXTRACT(YEAR FROM
(SELECT TO_DATE('1/21/2000', 'mm/dd/yyyy') FROM DUAL
)) AS YEAR
FROM DUAL
);
答案 2 :(得分:0)
select to_char(to_date('1/21/2000','mm/dd/yyyy'),'dd-mm-yyyy') from dual