我在FIRST_DATE列中有時間戳日期格式,我需要從某個小時中選擇時間段,例如。所有從 18:00 10.05.21 到 18:00 11.05.2021
問題是時間戳格式的日期列 - FIRST_DATE : 10/05/2020 0:00:03,000000 TIMESTAMP(6)
所以我嘗試使用它:
select
count(*)
from TABLE
where to_char(FIRST_DATE, 'HH24:MI')>='18:00'
因此,通過這種方式,我可以按時間限制開始時間段,但是如果我為此添加日期,我的條件將停止作業
and to_char(FIRST_DATE, 'DD-MON-YY')>='10-MAY-21'
我如何更正我的腳本以選擇所有從 18:00 10.05.21 到 18:00 11.05.2021
uj5u.com熱心網友回復:
不要將日期(或時間戳)與字串進行比較。'18:00'并且'10-MAY-21'是字串。使用具有適當格式掩碼的 TO_TIMESTAMP,例如(第 5 行和第 6 行):
SQL> with test (first_date) as
2 (select to_timestamp('10/05/2020 23:00:03,000000', 'dd/mm/yyyy hh24:mi:ss,ff3') from dual)
3 select *
4 from test
5 where first_date between to_timestamp('10/05/2020 18:00:00,000000', 'dd/mm/yyyy hh24:mi:ss,ff3')
6 and to_timestamp('11/05/2020 18:00:00,000000', 'dd/mm/yyyy hh24:mi:ss,ff3')
7 /
FIRST_DATE
---------------------------------------------------------------------------
10.05.20 23:00:03,000000000
SQL>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/379818.html
標籤:sql 甲骨文 oracle-sqldeveloper
