Re: Mechanics of Select

From: Bill Moran <wmoran(at)potentialtech(dot)com>
To: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
Cc: brian <brian(at)zijn-digital(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Mechanics of Select
Date: 2008-02-11 12:59:19
Message-ID: 20080211075919.6e537e18.wmoran@potentialtech.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In response to Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>:

> On Feb 11, 2008, at 12:43 AM, brian wrote:
> > Try:
> >
> > CREATE OR REPLACE FUNCTION first_day_next_month(inputdate date)
> > RETURNS date AS
> > $BODY$
> > DECLARE
> > resultdate date;
> > BEGIN
> > SELECT INTO resultdate to_date(to_char((inputdate + interval \
> > '1 month'), 'yyyy-MM') || '-01', 'yyyy-mm-dd');
> > RETURN resultdate;
> > END;
> > $BODY$
> > LANGUAGE 'plpgsql';
>
>
> No need for the variable or the SELECT, and it's an immutable
> function, so better define that. Besides that it's probably better to
> use the date_trunc function here.
>
> Try:
>
> CREATE OR REPLACE FUNCTION first_day_next_month(inputdate date)
> RETURNS date AS
> $BODY$
> BEGIN
> RETURN date_trunc('month', inputdate + interval '1 month');
> END;
> $BODY$
> LANGUAGE 'plpgsql' IMMUTABLE;
>
> And with that I wonder why you'd even need a function :)

Because it's clear what the function does by the name. It becomes
self-documenting, and ginormous queries will be easier to grok with
a function called first_day_next_month().

--
Bill Moran
http://www.potentialtech.com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ivan Sergio Borgonovo 2008-02-11 13:04:32 end of life for pg versions...
Previous Message Dawid Kuroczko 2008-02-11 12:04:31 pg_stat_activity xact_start and autovacuum