Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YvT7r-0008GV-IW for pgsql-performance@arkaria.postgresql.org; Thu, 21 May 2015 16:15:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YvT7r-0003o5-0s for pgsql-performance@arkaria.postgresql.org; Thu, 21 May 2015 16:15:15 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YvT7p-0003kz-JE for pgsql-performance@postgresql.org; Thu, 21 May 2015 16:15:13 +0000 Received: from tempest.mainecoon.com ([69.12.217.123]) by makus.postgresql.org with esmtps (TLS1.1:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1YvT7n-0000ZE-1j for pgsql-performance@postgresql.org; Thu, 21 May 2015 16:15:12 +0000 Received: from [10.1.1.2] (fw.boscorama.com [208.106.105.40]) by tempest.mainecoon.com (Postfix) with ESMTPSA id 5760C1E80570; Thu, 21 May 2015 09:15:09 -0700 (PDT) Message-ID: <555E0488.1030908@boscorama.com> Date: Thu, 21 May 2015 09:15:04 -0700 From: Bosco Rama User-Agent: Mozilla/5.0 (X11; Linux i686 on x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: "David G. Johnston" , "er.tejaspatel88@gmail.com" CC: "pgsql-performance@postgresql.org" Subject: Re: Fastest way / best practice to calculate "next birthdays" References: <481DE036.90706@commandprompt.com> <481DF76D.3060807@theendofthetunnel.de> <481E9E12.8070208@Sheeky.Biz> <1431941415888-5849705.post@n5.nabble.com> In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 05/20/15 20:22, David G. Johnston wrote: > On Monday, May 18, 2015, er.tejaspatel88@gmail.com < > er.tejaspatel88@gmail.com> wrote: > >> If I have to find upcoming birthdays in current week and the current week >> fall into different months - how would you handle that? >> > > Extract(week from timestamptz_column) > > ISO weeks are not affected by month boundaries but do start on Monday. There is the year start/end boundary conditions to worry about there. If the current week covers Dec28-Jan02 then week of year won't help for a birthday on Jan01 or Jan02 if 'today' is in the Dec portion. Ditto for birthday in Dec portion when 'today' is in the Jan portion. There is probably a better way to do it than what I'm showing here, but here's an example: with x as ( select now() - (extract(dow from now()) || ' days')::interval as weekstart ) select to_char(x.weekstart, 'YYYY-MM-DD') as first_day, to_char(x.weekstart + '6 days', 'YYYY-MM-DD') as last_day from x; You could probably make some of that into a function that accepts a timestamptz and generates the two days. Or even does the compare too. HTH. Bosco. -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance