Thread: Fun with Dates

Fun with Dates

From
Mark Fenbers
Date:
Greetings,<br /><br /> I want to be able to select all data going back to the beginning of the current month.  The
followingportion of an SQL does NOT work, but more or less describes what I want...<br /><br /> ... WHERE obstime >=
NOW()- INTERVAL (SELECT EXTRACT (DAY FROM NOW() ) ) + ' days'<br /><br /> In other words, if today is the 29th of the
month,I want to select data that is within 29 days old... WHERE obstime >= NOW() - INTERVAL '29 days'<br /><br />
Howdo I craft a query to do use a variable day of the month?<br /><br /> Mark<br /> 

Re: Fun with Dates

From
Thomas Kellerer
Date:
Mark Fenbers wrote on 29.10.2012 23:38:
> Greetings,
>
> I want to be able to select all data going back to the beginning of
> the current month.  The following portion of an SQL does NOT work,
> but more or less describes what I want...
>
> ... WHERE obstime >= NOW() - INTERVAL (SELECT EXTRACT (DAY FROM NOW()
> ) ) + ' days'
>
> In other words, if today is the 29th of the month, I want to select
> data that is within 29 days old... WHERE obstime >= NOW() - INTERVAL
> '29 days'
>

Or the other way round: anything that is equal or greater than the first
of the current month:

select ...
from foobar
where obstime >= date_trunc('month', current_date);

Thomas





Re: Fun with Dates

From
Mark Fenbers
Date:
<br /><blockquote cite="mid:k6n0qf$c8f$1@ger.gmane.org" type="cite">Or the other way round: anything that is equal or
greaterthan the first <br /> of the current month: <br /><br /> select ... <br /> from foobar <br /> where obstime
>=date_trunc('month', current_date); <br /></blockquote> I knew it had to be something simple!   thanks!<br />
Mark<br/>