oracle sql在一个查询中转换混合日期类型

时间:2017-03-11 21:05:58

标签: sql oracle

我只需要使用一个查询来转换所有" 11/19/15 6:18:00 PM"喜欢" 11/19/2015"在日期列中(Oracle sql语法)。任何人都可以帮我吗?

日期栏:

12/22/2016
01/17/2017
11/19/15 6:18:00 PM
5/14/13 8:38:00 PM

2 个答案:

答案 0 :(得分:0)

Oracle安装程序

CREATE TABLE your_table ( date_column ) AS (
  SELECT '12/22/2016'          FROM DUAL UNION ALL
  SELECT '1/17/2017'           FROM DUAL UNION ALL
  SELECT '11/19/15 6:18:00 PM' FROM DUAL UNION ALL
  SELECT '5/14/13 8:38:00 PM'  FROM DUAL;

<强>查询

SELECT CASE
         WHEN REGEXP_LIKE( date_column, '^\d{1,2}/\d{1,2}/\d{4}$' )
         THEN TO_DATE( date_column, 'MM/DD/YYYY' )
         WHEN REGEXP_LIKE( date_column, '^\d{1,2}/\d{1,2}/\d{2} [01]?\d:[0-5]?\d:[0-5]?\d [AP]M$' )
         THEN TO_DATE( date_column, 'MM/DD/YYYY HH12:MI:SS AM' )
       END AS date_value
FROM   your_table;

<强>输出

DATE_VALUE
-------------------
2016-12-22 00:00:00
2017-01-17 00:00:00
2015-11-19 18:18:00
2013-05-14 20:38:00

(注意:我的NLS_DATE_FORMAT设置为YYYY-MM-DD HH24:MI:SS - 这决定了我的会话中日期的格式。

答案 1 :(得分:0)

这给了我想要的东西:

v <- c("Toronto, ON", "Manchester, UK", "New York City, NY", "Newark, NJ", "Melbourne", "Los Angeles, CA", "New York, USA", "Liverpool, England", "Fort Collins, CO", "London, UK", "New York, NY")