Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset the date portion to the first of the month while preserving the time

Tags:

sql

oracle

Is there any way to reset the date portion to the first of the month while preserving the time? For example:

2018-01-02 23:00:00 -> 2018-01-01 23:00:00
2018-04-04 10:00:00 -> 2018-04-01 10:00:00
like image 291
Gadziu Avatar asked Apr 06 '18 17:04

Gadziu


1 Answers

with x as (
  select to_date( '2018-01-02 23:00:00', 'yyyy-mm-dd hh24:mi:ss') as d from dual
  union all
  select to_date(  '2018-04-04 10:00:00', 'yyyy-mm-dd hh24:mi:ss') from dual
)


SELECT d, d - trunc( d ) + trunc( d, 'MM' )
FROM x;

D                   D-TRUNC(D)+TRUNC(D,
------------------- -------------------
2018-01-02 23:00:00 2018-01-01 23:00:00
2018-04-04 10:00:00 2018-04-01 10:00:00
like image 179
krokodilko Avatar answered Oct 20 '22 18:10

krokodilko