tags:

views:

442

answers:

4

Hi,

How to get start date and end dates in a query from database.

Thanks,

Lico

A: 

Use trunc( ... ) and last_day( ... ).

See also: Oracle Forums.

Dave Jarvis
thanks for reply.it would be nice if I get query for H2 database
Lico
Sorry, the question was tagged Oracle when I looked. ;-)
Dave Jarvis
+1  A: 

CURRENT_DATE[()] | CURDATE() | SYSDATE | TODAY = returns current date

Start of month:

DATE EXTRACT(YEAR FROM SYSDATE) +'-'+ EXTRACT(MONTH FROM SYSDATE) +'-01'
OMG Ponies
Thanks for the reply. I want to get date from system rathor than a table.
Lico
Updated to suit.
OMG Ponies
I tried select DATEADD('MONTH', 1, SYSDATE)its giving me 2009-10-08 23:15:16.578...but I need first date (2009-10-01 00:00:00.000)of the current monthThanks!
Lico
I'm going on record: H2 SQL is limited
OMG Ponies
+1  A: 

http://sqltutorials.blogspot.com/2007/06/sql-first-and-last-day-of-month.html

Only thing that isn't covered is how to retrieve the current date... see rexem's post.

Mayo
Thanks a lot! Article explained what I needed to understand the logic behind for getting the desired date. Substract no. of days from current date.
Lico
A: 

For completeness, start of month:

DATE EXTRACT(YEAR FROM SYSDATE) +'-'+ EXTRACT(MONTH FROM SYSDATE) +'-01'

End of month:

DATEADD('DAY', -1, DATEADD('MONTH', 1, DATE EXTRACT(YEAR FROM SYSDATE) +'-'+ EXTRACT(MONTH FROM SYSDATE) +'-01'))
Cellfish