Re: Function Help - Mailing list pgsql-general

From Peter Eisentraut
Subject Re: Function Help
Date
Msg-id Pine.LNX.4.30.0109242304590.679-100000@peter.localdomain
Whole thread Raw
In response to Function Help  ("Brian C. Doyle" <bcdoyle@mindspring.com>)
Responses Re: Function Help
List pgsql-general
Brian C. Doyle writes:

> CREATE FUNCTION first_saturday(date)
> RETURNS date
> AS '
> Select CASE WHEN date_part(\'dow\',\'$1\'::DATE)=0 THEN date(\'$1\')+6
> WHEN date_part(\'dow\',\'$1\'::DATE)=1 THEN date(\'$1\')+5
> WHEN date_part(\'dow\',\'$1\'::DATE)=2 THEN date(\'$1\')+4
> WHEN date_part(\'dow\',\'$1\'::DATE)=3 THEN date(\'$1\')+3
> WHEN date_part(\'dow\',\'$1\'::DATE)=4 THEN date(\'$1\')+2
> WHEN date_part(\'dow\',\'$1\'::DATE)=5 THEN date(\'$1\')+1
> WHEN date_part(\'dow\',\'$1\'::DATE)=6 THEN date(\'$1\')+0
> END'LANGUAGE 'sql'
>
> I get an error that $1 is not a valid date.

Don't quote the $1.  E.g.,

WHEN date_part(\'dow\',$1)=6 THEN date($1)+0

The $1 etc. are not macros, they are identifiers representing a typed
expression.

--
Peter Eisentraut   peter_e@gmx.net   http://funkturm.homeip.net/~peter


pgsql-general by date:

Previous
From: Lamar Owen
Date:
Subject: Re: Make failure on v. 7.1.2
Next
From: "Colin 't Hart"
Date:
Subject: Re: [HACKERS] not on .hackers