Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle PLSQL truncate datetime to specific hours

I have an Oracle PLSQL code generating a list of datetime stamps and I would like to truncate them to the specific hours of 7am and 7pm rather than the beginning of the day.

For example:

  • 01/03/2013 0700 becomes 01/03/2013 0700
  • 01/03/2013 1235 becomes 01/03/2013 0700
  • 01/03/2013 1932 becomes 01/03/2013 1900
  • 02/03/2013 0612 becomes 01/03/2013 1900

My code is currently:

 SELECT  TRUNC(TRUNC(SYSDATE,'hh') + 1/24 - (ROWNUM) / 24, 'dd')  as shift_date
 FROM widsys.times
 ORDER BY SYSDATE

Thanks

like image 863
user2793907 Avatar asked Sep 19 '13 04:09

user2793907


People also ask

How do I TRUNC a timestamp?

Truncates a date, time, or timestamp to the specified part. For example, truncating a timestamp down to the quarter returns the timestamp corresponding to midnight of the first day of the original timestamp's quarter. Provides alternative syntax for DATE_TRUNC (reverses the two arguments).

How do I truncate a date and time in SQL?

In Oracle there is a function (trunc) used to remove the time portion of a date. In order to do this with SQL Server, you need to use the convert function. Convert takes 3 parameters, the datatype to convert to, the value to convert, and an optional parameter for the formatting style.

Can we use TRUNC for date in Oracle?

The TRUNC (date) function is used to get the date with the time portion of the day truncated to a specific unit of measure. It operates according to the rules of the Gregorian calendar. The date to truncate.

Can we use truncate in Plsql?

The TRUNC function is an inbuilt function in PLSQL which is used to return a number truncated to a particular number of decimal places.


2 Answers

Without conditionals :)

Select your_date, 
  trunc(your_date - 7/24) +                       --the date
  trunc(to_char(your_date - 7/24,'hh24')/12)/2 +  --wich half of day
  7/24                                            --shift the hour
from 
your_table;

See a fiddle.

like image 173
Florin stands with Ukraine Avatar answered Sep 30 '22 09:09

Florin stands with Ukraine


with data(time) as (
  select to_date('2013-09-19 00:00:00', 'YYYY-MM-DD HH24:MI:SS') from dual union all
  select to_date('2013-09-19 06:45:44', 'YYYY-MM-DD HH24:MI:SS') from dual union all
  select to_date('2013-09-19 08:12:25', 'YYYY-MM-DD HH24:MI:SS') from dual union all
  select to_date('2013-09-19 18:59:59', 'YYYY-MM-DD HH24:MI:SS') from dual union all
  select to_date('2013-09-19 19:00:00', 'YYYY-MM-DD HH24:MI:SS') from dual union all
  select to_date('2013-09-19 20:15:35', 'YYYY-MM-DD HH24:MI:SS') from dual union all
  select to_date('2013-09-19 23:59:59', 'YYYY-MM-DD HH24:MI:SS') from dual
)
select d.time,
case
  when to_number(to_char(d.time, 'HH24')) >= 19 then
    trunc(d.time) + 19/24
  when to_number(to_char(d.time, 'HH24')) >= 7 then
    trunc(d.time) + 7/24
  else
    trunc(d.time - 1) + 19/24
end as shift_date
from data d
;
like image 24
user272735 Avatar answered Sep 30 '22 07:09

user272735