2016-12-12 53 views
0

我有一個名爲request_date一個TIMESTAMP列的表。設定時間戳的時間部分爲固定值

我要選擇此列的值,但有以下條件:如果時間是19:00:00以後,那麼我應該選擇在次日08:00:00(當然,如果時間是08:00:00之前,我應該選擇那天的08:00:00)。

怎麼會是這樣SELECT子句中實現?

回答

1
SELECT CASE WHEN request_date < TRUNC(request_date) + INTERVAL '8' HOUR 
      THEN TRUNC(request_date) + INTERVAL '8' HOUR 
      WHEN request_date >= TRUNC(request_date) + INTERVAL '19' HOUR 
      THEN TRUNC(request_date) + INTERVAL '32' HOUR 
      ELSE request_date 
      END AS adjusted_request_date 
FROM table_name 

SELECT CASE WHEN EXTRACT(HOUR FROM request_date) < 8 
      THEN TRUNC(request_date) + INTERVAL '8' HOUR 
      WHEN EXTRACT(HOUR FROM request_date) >= 19 
      THEN TRUNC(request_date) + INTERVAL '32' HOUR 
      ELSE request_date 
      END AS adjusted_request_date 
FROM table_name 
1
select 
    case 
     when to_char(request_date,'HH24') >= 19 
     then to_timestamp(to_char(trunc(request_date) +1,'YYYYMMDD') || '0800','YYYYMMDDHH24MI') 
     when to_char(request_date,'HH24') < 8 
     then to_timestamp(to_char(trunc(request_date),'YYYYMMDD') || '0800','YYYYMMDDHH24MI') 
     else request_date 
    end as ReqStartTS