【问题标题】:Validate date in Oracle without using function [duplicate]在 Oracle 中验证日期而不使用函数 [重复]
【发布时间】:2014-08-03 13:19:21
【问题描述】:

我是甲骨文的新手。我必须在 oracle 数据库表的列中选择无效日期。但是由于在数据库中具有只读权限,我也无法编写函数。 任何人都可以帮助编写简单的查询来选择列中的无效日期:

例如从表中选择日期,to_date(dates 'yyyy/mm/dd')

在上述查询中,如果日期格式无效,则会出错。但是我必须输出那个日期而不是错误。我们可以在简单的查询中做到吗?

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    您可以使用正则表达式测试格式。

    应该是这样的:

    select dates
    from tbl
    where regexp_like(dates, '[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}')
    

    这工作正常。它检查格式是否为“4 位数字/2 位数字/2 位数字”。您可能想要更强大的东西,例如:

    select dates
    from tbl
    where regexp_like(dates, '[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}') or
          (substr(dates, 1, 4) not between '1900' and '2014' or
           substr(dates, 6, 2) not between '01' and '12' 
           substr(dates, 9, 2) not between '01' and '31'
          )
    

    这会检查每列中的格式和合理值。当然,它不会检查 6 月 31 日,但它会捕获很多错误。

    【讨论】:

      【解决方案2】:

      你可以试试:

      select *
      from tbl
      where not regexp_like (dtstring,'^[[:digit:]{4},/,[:digit:]{2},/,[:digit:]{2}]')
      

      小提琴: http://sqlfiddle.com/#!4/c7da3/11/0

      将显示任何不是 4 位数字的值,后跟一个斜线,再多 2 位数字,然后是一个斜线,再后跟 2 位数字。

      但它不会检查与该格式匹配的值是否无效,即。 2012/23/99

      【讨论】:

        猜你喜欢
        • 2018-08-16
        • 1970-01-01
        • 1970-01-01
        • 2012-10-14
        • 1970-01-01
        • 1970-01-01
        • 2013-03-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多