Oracle TO_CHAR() 函数中的错误
为什么以下查询的第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,它不在第 7 行。问题是“小时”在某种程度上被转换为字符串(to_char),但之后与日期进行比较,并且隐式转换并不总是有效。更正的查询:
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:
您在 WHERE 子句中将 DATE 与 CHAR 进行比较。这应该有效:
you're comparing DATEs to CHARs in the WHERE clause. This should work: