将时间戳的小时部分设置为固定值

时间:2016-12-12 10:51:30

标签: sql oracle

我有一个名为TIMESTAMP的{​​{1}}列的表格。

我想选择此列的值,但是具有以下条件:如果时间是在19:00:00之后,那么我应该在第二天选择08:00:00(当然,如果时间是08:00:00之前,我应该选择那天的08:00:00。

如何在select子句中实现?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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