1

我有一个包含日期类型字段的表,它包含很少的带有时间部分的日期值的记录,并且很少有记录只有没有时间部分的日期值。如何检查哪些记录有时间部分,而不是逐个记录导航?

这是我目前正在使用的,但我想要更快的东西:

SELECT MY_TAB.SEQ, MY_TAB.CRTE_DT,
       CASE WHEN TRUNC (MY_TAB.CRTE_DT) = MY_TAB.CRTE_DT
       THEN 'False' ELSE 'True' END AS "Has Time Portion"
FROM (SELECT 1 AS SEQ,
             TO_DATE ('7/28/2013', 'MM/DD/YYYY') AS CRTE_DT
      FROM DUAL
      UNION
      SELECT 2 AS SEQ,
             TO_DATE ('11/07/2013 12:27:54', 'MM/DD/YYYY HH24:MI:SS') AS CRTE_DT
      FROM DUAL) MY_TAB;
4

3 回答 3

1

不错的解决方案。只抓一个。如果时间戳是午夜 12:00,则结果将为 false。

TO_DATE ('11/07/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS') 将屈服于 false

于 2013-11-08T17:42:30.313 回答
1

CREATE TABLE MY_TAB AS SELECT SEQ, CRTE_DT FROM (SELECT 1 AS SEQ, TO_DATE ('7/28/2013', 'MM/DD/YYYY') AS CRTE_DT FROM DUAL UNION SELECT 2 AS SEQ, TO_DATE ('11/07/ 2013 12:27:54', 'MM/DD/YYYY HH24:MI:SS') 作为 CRTE_DT 从双联合选择 3 作为 SEQ, TO_DATE ('09/5/2013 00:00:00', 'MM/DD /YYYY HH24:MI:SS') AS CRTE_DT FROM DUAL UNION SELECT 4 AS SEQ, TO_DATE ('4/15/2013 00:00:01', 'MM/DD/YYYY HH24:MI:SS') AS CRTE_DT FROM DUAL UNION SELECT 5 AS SEQ, TO_DATE ('12/12/2012 1:01:01', 'MM/DD/YYYY HH24:MI:SS') AS CRTE_DT FROM DUAL);

SELECT MY_TAB.SEQ, MY_TAB.CRTE_DT, CASE WHEN TRUNC (MY_TAB.CRTE_DT) = MY_TAB.CRTE_DT THEN 'False' ELSE 'True' END AS "有时间部分" FROM MY_TAB;

于 2013-11-08T18:28:29.780 回答
0

如果您只想检查数据字段是否包含时间部分,您可以通过将会话更改为,

SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'dd/mm/yyyy';

Session altered.

SQL> SELECT SYSDATE FROM dual;

SYSDATE
----------
08/11/2013

SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'dd/mm/yyyy hh24:mi:ss';

Session altered.

SQL> SELECT SYSDATE FROM dual;

SYSDATE
-------------------
08/11/2013 08:30:49     
于 2013-11-08T03:03:05.147 回答