sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Francisco Reyes's Avatar

Date variables in psql


Question posted by: Francisco Reyes (Guest) on November 23rd, 2005 02:26 AM
Trying the following simple sql file:
\set proc_date 6/30/2004
\echo Date is :proc_date
select * from feeds where date = :proc_date limit 20;

If I start psql with the "-a" option I see the output:
\set proc_date 6/30/2004
\echo Date is :proc_date
Date is 6/30/2004
select * from feeds where date = :proc_date limit 20;

and get no records
If I type the exact same query manually it workds
select * from feeds where date = '6/30/2004' limit 20;

Any ideas of the variable may not be recognized on the select statement?

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to Join Bytes!

2 Answers Posted
Tom Lane's Avatar
Guest - n/a Posts
#2: Re: Date variables in psql

Francisco Reyes <lists@natserv.com> writes:[color=blue]
> Trying the following simple sql file:
> \set proc_date 6/30/2004
> \echo Date is :proc_date
> select * from feeds where date = :proc_date limit 20;[/color]

That's going to expand to

select * from feeds where date = 6/30/2004 limit 20;

whereas what you need is

select * from feeds where date = '6/30/2004' limit 20;

It's fairly painful to get single quotes into a psql variable;
AFAIK you have to do it like this:

\set proc_date '\'6/30/2004\''

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Francisco Reyes's Avatar
Guest - n/a Posts
#3: Re: Date variables in psql

On Thu, 7 Oct 2004, Tom Lane wrote:
[color=blue]
> It's fairly painful to get single quotes into a psql variable;
> AFAIK you have to do it like this:
> \set proc_date '\'6/30/2004\''[/color]


Thanks that worked.
I figure I needed to escape the single quotes, but I had tried
\'6/30/2004\', which did not work.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

 
Not the answer you were looking for? Post your question . . .
196,947 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,947 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors