Re: Function Help - Mailing list pgsql-general

From merlyn@stonehenge.com (Randal L. Schwartz)
Subject Re: Function Help
Date
Msg-id m1bsk0ru3k.fsf@halfdome.holdit.com
Whole thread Raw
In response to Function Help  ("Brian C. Doyle" <bcdoyle@mindspring.com>)
Responses Re: Function Help
List pgsql-general
>>>>> "Brian" == Brian C Doyle <bcdoyle@mindspring.com> writes:

Brian> Hello all,
Brian> I am working on a function to determine the date of the first saturday
Brian> of the month.

Brian> Currently I have:

Brian> CREATE FUNCTION first_saturday(date)
Brian> RETURNS date
Brian> AS '
Brian> Select CASE WHEN date_part(\'dow\',\'$1\'::DATE)=0 THEN date(\'$1\')+6
Brian> WHEN date_part(\'dow\',\'$1\'::DATE)=1 THEN date(\'$1\')+5
Brian> WHEN date_part(\'dow\',\'$1\'::DATE)=2 THEN date(\'$1\')+4
Brian> WHEN date_part(\'dow\',\'$1\'::DATE)=3 THEN date(\'$1\')+3
Brian> WHEN date_part(\'dow\',\'$1\'::DATE)=4 THEN date(\'$1\')+2
Brian> WHEN date_part(\'dow\',\'$1\'::DATE)=5 THEN date(\'$1\')+1
Brian> WHEN date_part(\'dow\',\'$1\'::DATE)=6 THEN date(\'$1\')+0
Brian> END'LANGUAGE 'sql'

Brian> I get an error that $1 is not a valid date.  But I want that to be the
Brian> variable I enter...
Brian> what Am I doing wrong???

Working too hard? :)

why not just:

My_Col + 6 - date_part('dow', My_Col)

Don't even need a function for that. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

pgsql-general by date:

Previous
From: "Brian C. Doyle"
Date:
Subject: Re: Function Help
Next
From: "Brian C. Doyle"
Date:
Subject: Re: Function Help