Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 464924768B8 for ; Fri, 21 Jun 2002 09:22:48 -0400 (EDT) Received: from smtp013.mail.yahoo.com (smtp013.mail.yahoo.com [216.136.173.57]) by postgresql.org (Postfix) with SMTP id 2F3F8475CE6 for ; Fri, 21 Jun 2002 09:22:27 -0400 (EDT) Received: from psc.progress.com (HELO Yahoo.com) (janwieck@192.233.92.200 with plain) by smtp.mail.vip.sc5.yahoo.com with SMTP; 21 Jun 2002 13:22:30 -0000 Message-ID: <3D132893.8D0CBF55@Yahoo.com> Date: Fri, 21 Jun 2002 09:22:27 -0400 From: Jan Wieck X-Mailer: Mozilla 4.79 [en] (X11; U; Linux 2.2.14-6.1.1smp i686) X-Accept-Language: en MIME-Version: 1.0 To: Bruno Wolff III Cc: Michael Agbaglo , josh@agliodbs.com, pgsql-sql@postgresql.org Subject: Re: how to sort a birthday list ? References: <3D122508.70104@shifted-bytes.de> <200206201703.52435.josh@agliodbs.com> <3D12E43E.3090307@shifted-bytes.de> <20020621123919.GA11968@wolff.to> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Archive-Number: 200206/292 X-Sequence-Number: 8257 Bruno Wolff III wrote: > > On Fri, Jun 21, 2002 at 10:30:54 +0200, > Michael Agbaglo wrote: > > > > of course you could sort by DOY but then you'll have a problem w/ the > > next year: > > > > if it's let's say december and you select the list for the next 60 days, > > persons having birthday in december will appear after persons having > > birthday in january. > > > > I tried to use CASE WHEN... THEN in ORDER BY but it doesn't seem to work > > (syntax error at '') > > You also need to worry about leap years. If a birthday is February 29 > and there isn't one this year, what do you want to happen? You can create a little PL/pgSQL function like this: CREATE FUNCTION next_birthday(date) RETURNS date AS ' DECLARE p_dob ALIAS FOR $1; v_age integer; v_birthday date; BEGIN -- First we get the age in years v_age := EXTRACT (YEAR FROM CURRENT_DATE) - EXTRACT (YEAR FROM p_dob); -- We add that to the DOB to get this years birthday v_birthday := p_dob + (v_age::text || '' years'')::interval; -- If that is in the past, we add another year IF v_birthday < CURRENT_DATE THEN v_birthday := v_birthday + ''1 year''::interval; END IF; RETURN v_birthday; END;' LANGUAGE plpgsql; It just calculates the next birthday of a person relative from today. Then query with SELECT next_birthday(birthday), name FROM person ORDER BY 1; Jan -- #======================================================================# # It's easier to get forgiveness for being wrong than for being right. # # Let's break this rule - forgive me. # #================================================== JanWieck@Yahoo.com #