Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return 14th day from month which was 2 months ago with determinated time in Oracle SQL?

How can I take the 14th day from month which was 2 months ago with time 23:59:59 in Oracle SQL ?
I have query like below and I need answer something like below (similar style):

select to_char(to_date(LAST_DAY(ADD_MONTHS(sysdate, -2)), 'YY-MM-DD'), 'YY/MM/DD) from dual

Above query return: 21/12/31, nevertheless I need query like above which will return value: '21/12/14 23:59:59'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use:

SELECT ADD_MONTHS(TRUNC(sysdate, 'MM'), -2)
         + INTERVAL '13 23:59:59' DAY TO SECOND  AS day
FROM   DUAL

Which outputs:

DAY
2021-12-14 23:59:59

If you want formatted with a specific format then use TO_CHAR:

SELECT TO_CHAR(
         ADD_MONTHS(TRUNC(sysdate, 'MM'), -2)
           + INTERVAL '13 23:59:59' DAY TO SECOND,
         'YYYY-MM-DD HH24:MI:SS'
       ) AS day
FROM   DUAL

db<>fiddle here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading