Function Help

Started by Brian C. Doyleover 24 years ago5 messagesgeneral
Jump to latest
#1Brian C. Doyle
bcdoyle@mindspring.com

Hello all,

I am working on a function to determine the date of the first saturday of
the month.

Currently I have:

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. But I want that to be the
variable I enter...
what Am I doing wrong???

#2Peter Eisentraut
peter_e@gmx.net
In reply to: Brian C. Doyle (#1)
Re: Function Help

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

#3Brian C. Doyle
bcdoyle@mindspring.com
In reply to: Peter Eisentraut (#2)
Re: Function Help

That was it... I knew it was something simple.. Thanks Peter!!!

At 11:06 PM 9/24/01 +0200, Peter Eisentraut wrote:

Show quoted text

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

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

#4Randal L. Schwartz
merlyn@stonehenge.com
In reply to: Brian C. Doyle (#1)
Re: Function Help

"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/&gt;
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

#5Brian C. Doyle
bcdoyle@mindspring.com
In reply to: Randal L. Schwartz (#4)
Re: Function Help

The main reason is that I am not quering a table with it as of yet...
Ultimately it will before query that is a Month do date query that does not
use a calender month

At 05:29 PM 9/24/01 -0700, Randal L. Schwartz wrote:

Show quoted text

"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/&gt;
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!