Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id DA739B5DBF4 for ; Tue, 11 Oct 2011 13:19:56 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 91294-02 for ; Tue, 11 Oct 2011 16:19:50 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sam.nabble.com (sam.nabble.com [216.139.236.26]) by mail.postgresql.org (Postfix) with ESMTP id 6379CB5DBE3 for ; Tue, 11 Oct 2011 13:19:50 -0300 (ADT) Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1RDf3U-0004zy-QS for pgsql-sql@postgresql.org; Tue, 11 Oct 2011 09:19:48 -0700 Date: Tue, 11 Oct 2011 09:19:48 -0700 (PDT) From: skdangi To: pgsql-sql@postgresql.org Message-ID: <1318349988812-4892306.post@n5.nabble.com> In-Reply-To: <20051107140036.11DC6D6873@svr1.postgresql.org> References: <20051107140036.11DC6D6873@svr1.postgresql.org> Subject: Re: Extract week from date, start with sunday MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.767 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, T_TO_NO_BRKTS_FREEMAIL=0.01, URI_HEX=1.122 X-Spam-Level: X-Archive-Number: 201110/12 X-Sequence-Number: 35896 You can use the below query to get the week from Sunday. select ceil(((now()::date -((CAST(EXTRACT (year FROM now()) as text)||'-01-04')::date - CAST (EXTRACT (isodow FROM (CAST(EXTRACT (year FROM now()) as text)||'-01-04')::date) as integer) ))+1)/7.0) While using it you need to replace the now() with the date value you want to compute the week number. Explanation: 1)(no of days between the date provided and first day of first week of the year +1)/7.0 First week of the year contains 4th January (refer Note). So first day of first week = first day of the week in which 4th January belongs. 2)Finally take the ceiling of the result. Note:The number of the week of the year that the day is in. By definition (ISO 8601), the first week of a year contains January 4 of that year. -- View this message in context: http://postgresql.1045698.n5.nabble.com/Extract-week-from-date-start-with-sunday-tp2144144p4892306.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com.