Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 9C1D56335D5 for ; Fri, 26 Mar 2010 08:47:27 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 05636-10 for ; Fri, 26 Mar 2010 11:47:09 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mx4.wp.pl (mx4.wp.pl [212.77.101.8]) by mail.postgresql.org (Postfix) with ESMTP id 614646323CE for ; Fri, 26 Mar 2010 08:47:16 -0300 (ADT) Received: (wp-smtpd smtp.wp.pl 23323 invoked from network); 26 Mar 2010 12:47:14 +0100 Received: from aazd173.neoplus.adsl.tpnet.pl (HELO [127.0.0.1]) (ipluta@[83.6.141.173]) (envelope-sender ) by smtp.wp.pl (WP-SMTPD) with AES256-SHA encrypted SMTP for ; 26 Mar 2010 12:47:14 +0100 Message-ID: <4BAC9EC1.7010708@wp.pl> Date: Fri, 26 Mar 2010 12:47:13 +0100 From: Ireneusz Pluta User-Agent: Thunderbird 2.0.0.24 (Windows/20100228) MIME-Version: 1.0 To: Sergey Konoplev CC: pgsql-sql@postgresql.org Subject: Re: Week to date function References: <4BAB2C15.8090406@wp.pl> <4BAC9107.5070701@wp.pl> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-WP-AV: skaner antywirusowy poczty Wirtualnej Polski S. A. X-WP-SPAM: NO 0000000 [8VNk] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.299 tagged_above=-10 required=5 tests=AWL=1.300, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201003/206 X-Sequence-Number: 33913 Sergey Konoplev pisze: >> CREATE OR REPLACE FUNCTION your_week2date(double precision, double >> precision) RETURNS SETOF date >> AS >> $_$ >> SELECT day >> FROM ( >> SELECT ( >> date_trunc('week', ($1::text||'-01-01')::date) >> + ($2::text||' week')::interval >> + ( d::text||' day')::interval >> )::date AS day >> FROM generate_series(0, 6) AS d >> ) alias >> WHERE to_char(day, 'IYYY')::integer = $1 >> ORDER >> BY 1 >> $_$ >> LANGUAGE SQL >> IMMUTABLE >> > > I think it is better to use date_part('year', day) instead of > to_char(...). this might cut first or last week in the year, like this: SELECT your_week2date(2009, 52) ; your_week2date ---------------- 2009-12-28 2009-12-29 2009-12-30 2009-12-31 2010-01-01 cut 2010-01-02 cut 2010-01-03 cut (7 rows) which is not what I want when playing with isoweeks. > And may be it is worth to do raise exception when > incorrect week specified. > but, maybe, controlled by an additonal parameter saying if one prefers to have exception or rather an empty resultset