Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id A2D396327A2 for ; Fri, 26 Mar 2010 08:02:34 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 17495-08 for ; Fri, 26 Mar 2010 11:02:23 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ww0-f46.google.com (mail-ww0-f46.google.com [74.125.82.46]) by mail.postgresql.org (Postfix) with ESMTP id 01F986323B7 for ; Fri, 26 Mar 2010 08:02:23 -0300 (ADT) Received: by wwe15 with SMTP id 15so6526634wwe.19 for ; Fri, 26 Mar 2010 04:02:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:received:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=OIDIuZm7GAlG6CBe80HjMFX1ZRBh6kt2/sppKLctsuE=; b=WYf/KMyHWHZuVci//a1f31ADpylFcU5LtKQlq4/Rax1wCqXzDRYQFqYL/2kA3NovEn FZgS7Y5sGOo8bLgZjbPeLxWXxym0cH2pmBVOULIgzGtIqkcj9i6N2dwVduXITHLNFPb+ TwnkpG1tS0a+nml2nx39Ul4BXc+RdPHinHxb8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=q0g2bAco70qM5El9MI+nd5WCzo//fPEKRVSFZF2XZUSOFNUPBCX3CVNJWQR4X5vqjL 6oqJyJsF80aws5GwJMaUQi3+SyjUgzf9GZZ5V31qHXccVL7KvvEd9wuOAa7ibEfh5Bxn B133MUO78bwblCHh11URb9LTXTEM15OCQLrq0= MIME-Version: 1.0 Received: by 10.216.153.136 with HTTP; Fri, 26 Mar 2010 04:02:02 -0700 (PDT) In-Reply-To: <4BAC9107.5070701@wp.pl> References: <4BAB2C15.8090406@wp.pl> <4BAC9107.5070701@wp.pl> From: Sergey Konoplev Date: Fri, 26 Mar 2010 14:02:02 +0300 Received: by 10.216.160.213 with SMTP id u63mr354285wek.128.1269601342141; Fri, 26 Mar 2010 04:02:22 -0700 (PDT) Message-ID: Subject: Re: Week to date function To: Ireneusz Pluta Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201003/205 X-Sequence-Number: 33912 > CREATE OR REPLACE FUNCTION your_week2date(double precision, double > precision) RETURNS SETOF date > AS > $_$ > SELECT =A0day > FROM ( > =A0 SELECT =A0( > =A0 =A0 =A0 =A0 =A0 =A0 =A0 date_trunc('week', ($1::text||'-01-01')::date= ) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 + ($2::text||' week')::interval > =A0 =A0 =A0 =A0 =A0 =A0 =A0 + ( d::text||' =A0day')::interval > =A0 =A0 =A0 =A0 =A0 )::date =A0AS =A0day > =A0 FROM =A0 =A0generate_series(0, 6) AS d > ) =A0 alias > WHERE =A0 to_char(day, 'IYYY')::integer =3D $1 > ORDER > BY =A0 =A0 =A01 > $_$ > LANGUAGE SQL > IMMUTABLE I think it is better to use date_part('year', day) instead of to_char(...). And may be it is worth to do raise exception when incorrect week specified. --=20 Sergey Konoplev Blog: http://gray-hemp.blogspot.com / Linkedin: http://ru.linkedin.com/in/grayhemp / JID/GTalk: gray.ru@gmail.com / Skype: gray-hemp / ICQ: 29353802