如何识别无效日期并替换默认日期

时间:2014-10-13 06:41:42

标签: oracle oracle11g

如何识别插入日期的格式是否正确。 sample_date列数据类型在varchar中。在这里,我插入了两个值' 25-02-1999'和' 2004'。当我在sample_date列中找到无效日期时,我想用一些虚拟日期替换。

Example: 

        create table sample(sample_date varchar2(10 char));
        insert into sample values('25-02-1988');
        insert into sample values('2004')
        insert into sample values('2005')
        insert into sample values('2006')
        I want to replace 2004,2005,2006 with dummy date('01-01-1988')

1 个答案:

答案 0 :(得分:0)

将您的值存储为DATE数据类型,所有与日期相关的问题都会变得更加简单......

CREATE OR REPLACE TABLE sample(sample_date DATE);

INSERT INTO sample (sample_date) VALUES (TO_DATE('25-02-1988', 'DD-MM-YYYY'));

INSERT INTO sample (sample_date) VALUES (TO_DATE('2004', 'YYYY'));

INSERT INTO sample (sample_date) VALUES (TO_DATE('2005', 'YYYY'));

INSERT INTO sample (sample_date) VALUES (TO_DATE('2006', 'YYYY'));

如果您想以DD-MM-YYYY格式查看日期:

SELECT TO_CHAR(sample_date, 'DD-MM-YYYY') AS formatted_sample_date
  FROM sample;

希望它有所帮助...