我比較的日期SYSDATE時收到以下錯誤:ORA-01850:時間必須介於0和23甲骨文:比較日期時ORA-01850至SYSDATE
這裏是我的查詢:
with cr as
(select '1-2' heures from dual)
select mydate, to_date(mydate, 'yyyymmddhh24mi')
from (select to_char(extract(year from sysdate), 'fm0009') ||
to_char(extract(month from sysdate), 'fm09') ||
to_char(extract(day from sysdate), 'fm09') ||
to_char(h2.heure, 'fm09') || '45' mydate
from (select to_number(h.intervalle_debut + i.l) heure
from (select to_number(regexp_substr(cr.heures,
'[^-]+',
1,
1)) intervalle_debut,
to_number(regexp_substr(cr.heures,
'[^-]+',
1,
2)) intervalle_fin
from cr) h,
(select level - 1 l from dual connect by level <= 24) i
where h.intervalle_fin - h.intervalle_debut >= i.l) h2)
where to_date(mydate, 'yyyymmddhh24mi') > sysdate;
小的解釋:
子查詢 「H2」 返回這兩行:
heure
------
1
2
它對應於「cr」子查詢中給出的範圍1-2。 h2使用返回的小時數創建與今天相對應的日期。它還返回這兩行:
mydate
-------
201708030145
201708030245
日期看起來不錯。小時是1和2(這是正確的!)。
執行完整的查詢,而無需在where子句中返回這兩個日期:
TO_DATE(MYDATE,'YYYYMMDDHH24MI
------------------------------
03/08/2017 01:45:00
03/08/2017 02:45:00
這仍然是正確的。 但是,當添加where子句「to_date(mydate,'yyyymmddhh24mi')> sysdate」時,我得到了ORA-01850。
怎麼了?
你爲什麼要存儲在'varchar'列的日期? –
將'sysdate'轉換爲字符值的複雜方法可以簡化爲'to_char(sysdate,'yyyymmddhh42')||'45'' –
而不是'to_char(提取(從sysdate年),'fm0009')| | to_char(提取(sysdate中的月份),'fm09')|| to_char(提取(sysdate中的日期),'fm09')|| to_char(h2.heure,'fm09')|| '45'mydate'你可以簡化'TO_CHAR(TRUNC(sysdate)+ h2.heure/24 + 45/24/60,'yyyymmddhh24mi')' –