dbTalk Databases Forums  

Finding last day in a month

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss Finding last day in a month in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Paul Makepeace
 
Posts: n/a

Default Finding last day in a month - 10-19-2004 , 07:18 AM






This is perhaps more of a style question. I'm trying to find the 'right'
way with date/time functions to pull out the last day in a month from a
set of data that might contain rows that don't fall on the actual last
date (e.g. Friday 2004-07-30)

Foo.info_at("2004-07") # contains some logic to make "2004-07-%" for:

WHERE foo_date LIKE ? ORDER BY foo_date DESC LIMIT 1

This feels quite hacky to me for some reason but at least seems to work.

(I'd originally had foo_date = ?::date+'1month'::interval-'1day'::interval !)

Paul


--
Paul Makepeace .............................. http://paulm.com/inchoate/

"What is do I smell? A thousand paths to choose."
-- http://paulm.com/toys/surrealism/

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


Reply With Quote
  #2  
Old   
Doug Silver
 
Posts: n/a

Default Re: Finding last day in a month - 10-19-2004 , 01:40 PM






On Tuesday 19 October 2004 05:18 am, Paul Makepeace wrote:
Quote:
This is perhaps more of a style question. I'm trying to find the 'right'
way with date/time functions to pull out the last day in a month from a
set of data that might contain rows that don't fall on the actual last
date (e.g. Friday 2004-07-30)

Foo.info_at("2004-07") # contains some logic to make "2004-07-%" for:

WHERE foo_date LIKE ? ORDER BY foo_date DESC LIMIT 1

This feels quite hacky to me for some reason but at least seems to work.

(I'd originally had foo_date = ?::date+'1month'::interval-'1day'::interval
!)

Paul
Paul -

Just another suggestion as this probably isn't 'right' either, but it's what I
came up with to figure out the number of days in the previous month (feel
free to tell me a better way if there is one):

select date_part('days',current_date::date -
cast( date_part('days',current_date)||' days' as interval));

-doug

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.