0

I need to automate a report that will always the first day of the current month and year in the WHERE caluses.

"YYYY-MM-01"

I am trying to achieve the following but sadly, I can't get anything to work using To_Char/ To_Date/SysDate.

    to_date(SYSDATE('YYYY-MM') + '-01', 'yyyy-mm-dd')

Thank you

3
  • So you just want the first day of the current month? Commented Feb 28, 2014 at 16:04
  • I don't have an Oracle box here to test but SYSDATE does not accept parameters. You should be getting an error message :-? Commented Feb 28, 2014 at 16:09
  • Thanks everyone. Both proposed solution by (Joe / Gordon Linoff) are working as expected. Commented Feb 28, 2014 at 17:44

2 Answers 2

5

To get the first day of the current month (as a DATE), simply:

trunc(sysdate,'MON')

or, formatted:

to_char(trunc(sysdate,'MON'),'yyyy-mm-dd')
Sign up to request clarification or add additional context in comments.

Comments

1

The string concatenation operator in Oracle is ||, not +. And SYSDATE is a constant not a function:

to_date(to_char(SYSDATE, 'YYYY-MM') || '-01', 'yyyy-mm-dd')

By the way, you can also do this calculation as:

trunc(sysdate - extract(day from sysdate) + 1)

That is, subtract the day of the month and add one. That way, you don't have to mess with fiddly date/character formats.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.