Oracle TO_CHAR() 函数中的错误

发布于 2024-08-10 12:04:48 字数 646 浏览 11 评论 0原文

为什么以下查询的第 7 行会出现以下错误?

错误:ORA-01861:文字与格式字符串不匹配

查询:

01: SELECT hour
02: FROM (WITH all_hours AS
03:             (SELECT TO_DATE ('2000-01-01', 'yyyy-mm-dd') 
04:                      + NUMTODSINTERVAL (LEVEL - 1, 'hour') hour
05:              FROM DUAL
06:              CONNECT BY LEVEL <= 1000000)
07:      SELECT TO_CHAR (h.hour, 'yyyy-mm-dd hh:mi am') hour
08:      FROM all_hours h
09:      GROUP BY h.hour)
10: WHERE hour BETWEEN TO_DATE ('2009-11-10 01:00 am', 'yyyy-mm-dd hh:mi am')
11:                AND TO_DATE ('2009-11-10 01:00 am', 'yyyy-mm-dd hh:mi am')

Why does the following error occur on Line 7 of the query below?

Error: ORA-01861: literal does not match format string

Query:

01: SELECT hour
02: FROM (WITH all_hours AS
03:             (SELECT TO_DATE ('2000-01-01', 'yyyy-mm-dd') 
04:                      + NUMTODSINTERVAL (LEVEL - 1, 'hour') hour
05:              FROM DUAL
06:              CONNECT BY LEVEL <= 1000000)
07:      SELECT TO_CHAR (h.hour, 'yyyy-mm-dd hh:mi am') hour
08:      FROM all_hours h
09:      GROUP BY h.hour)
10: WHERE hour BETWEEN TO_DATE ('2009-11-10 01:00 am', 'yyyy-mm-dd hh:mi am')
11:                AND TO_DATE ('2009-11-10 01:00 am', 'yyyy-mm-dd hh:mi am')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

行至春深 2024-08-17 12:04:48

好吧,它不在第 7 行。问题是“小时”在某种程度上被转换为字符串(to_char),但之后与日期进行比较,并且隐式转换并不总是有效。更正的查询:

SELECT hour
FROM (WITH all_hours AS
            (
            SELECT TO_DATE ('2000-01-01', 'yyyy-mm-dd') 
                     + NUMTODSINTERVAL (LEVEL - 1, 'hour') hour
             FROM DUAL
             CONNECT BY LEVEL <= 1000000
             )
     SELECT TO_CHAR (h.hour, 'yyyy-mm-dd hh:mi am') hour, h.hour as hourdate
     FROM all_hours h
     GROUP BY h.hour)
WHERE hourdate BETWEEN TO_DATE ('2009-11-10 01:00 am', 'yyyy-mm-dd hh:mi am')
               AND TO_DATE ('2009-11-10 01:00 am', 'yyyy-mm-dd hh:mi am')

Well, it is not on the line 7. Problem is that "hour" on some level is converted to string (to_char), but after that is compared to the date, and implicit conversion does not work always. Corrected query:

SELECT hour
FROM (WITH all_hours AS
            (
            SELECT TO_DATE ('2000-01-01', 'yyyy-mm-dd') 
                     + NUMTODSINTERVAL (LEVEL - 1, 'hour') hour
             FROM DUAL
             CONNECT BY LEVEL <= 1000000
             )
     SELECT TO_CHAR (h.hour, 'yyyy-mm-dd hh:mi am') hour, h.hour as hourdate
     FROM all_hours h
     GROUP BY h.hour)
WHERE hourdate BETWEEN TO_DATE ('2009-11-10 01:00 am', 'yyyy-mm-dd hh:mi am')
               AND TO_DATE ('2009-11-10 01:00 am', 'yyyy-mm-dd hh:mi am')
妳是的陽光 2024-08-17 12:04:48

您在 WHERE 子句中将 DATE 与 CHAR 进行比较。这应该有效:

SQL> SELECT TO_CHAR(hour, 'yyyy-mm-dd hh:mi am')
  2    FROM (WITH all_hours AS (SELECT TO_DATE('2000-01-01', 'yyyy-mm-dd')
  3                              + NUMTODSINTERVAL(LEVEL - 1, 'hour') hour
  4                               FROM DUAL
  5                             CONNECT BY LEVEL <= 1000000)
  6          SELECT hour FROM all_hours h GROUP BY h.hour)
  7   WHERE hour BETWEEN TO_DATE('2009-11-10 01:00 am', 'yyyy-mm-dd hh:mi am')
  8          AND TO_DATE('2009-11-10 01:00 am', 'yyyy-mm-dd hh:mi am')
  9  ;

TO_CHAR(HOUR,'YYYY-MM-DDHH:MIA
------------------------------
2009-11-10 01:00 am

you're comparing DATEs to CHARs in the WHERE clause. This should work:

SQL> SELECT TO_CHAR(hour, 'yyyy-mm-dd hh:mi am')
  2    FROM (WITH all_hours AS (SELECT TO_DATE('2000-01-01', 'yyyy-mm-dd')
  3                              + NUMTODSINTERVAL(LEVEL - 1, 'hour') hour
  4                               FROM DUAL
  5                             CONNECT BY LEVEL <= 1000000)
  6          SELECT hour FROM all_hours h GROUP BY h.hour)
  7   WHERE hour BETWEEN TO_DATE('2009-11-10 01:00 am', 'yyyy-mm-dd hh:mi am')
  8          AND TO_DATE('2009-11-10 01:00 am', 'yyyy-mm-dd hh:mi am')
  9  ;

TO_CHAR(HOUR,'YYYY-MM-DDHH:MIA
------------------------------
2009-11-10 01:00 am
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文