what's wrong with my date comparison?
what's wrong with my date comparison?
Hi Everybody,
I have a table with a column of timestamp type. It is
known to postgres like this:
name | character varying | not null
value | character varying | not null
datecreated | timestamp without time zone | not null
when I do query
select name, value, datecreated
from mytable
where datecreated > 2007-10-02;
it reports:
name | value | datecreated
----------+----------------------+-------------------------
al_qual | 0 | 2007-08-09 00:06:06.742
srehquan | 3 | 2007-08-09 00:06:06.742
complete | 1 | 2007-08-09 00:06:06.743
al_quan | 0.315924933 | 2007-08-09 00:06:06.742
bsa_qual | 0 | 2007-08-09 00:06:06.743
bsl_qual | 2 | 2007-08-09 00:06:06.743
sh_qual | 0 | 2007-08-09 00:06:06.742
. . . .
. . . .
I don't understand why it thinks August is greater than
October. Can someone please elucidate what is going on?
Regards,
Tena Sakai
tsakai@gallo.ucsf.edu
Re: what's wrong with my date comparison?
Oooops! I got it.
I was missing quotes.
It must have evaluated 2007-10-02 and used it as a
numerical constant 1995.
Sorry about commotion.
Tena
-----Original Message-----
From: pgsql-sql-owner@postgresql.org on behalf of Tena Sakai
Sent: Tue 10/16/2007 10:57 AM
To: pgsql-sql@postgresql.org
Subject: [SQL] what's wrong with my date comparison?
Hi Everybody,
I have a table with a column of timestamp type. It is
known to postgres like this:
name | character varying | not null
value | character varying | not null
datecreated | timestamp without time zone | not null
when I do query
select name, value, datecreated
from mytable
where datecreated > 2007-10-02;
it reports:
name | value | datecreated
----------+----------------------+-------------------------
al_qual | 0 | 2007-08-09 00:06:06.742
srehquan | 3 | 2007-08-09 00:06:06.742
complete | 1 | 2007-08-09 00:06:06.743
al_quan | 0.315924933 | 2007-08-09 00:06:06.742
bsa_qual | 0 | 2007-08-09 00:06:06.743
bsl_qual | 2 | 2007-08-09 00:06:06.743
sh_qual | 0 | 2007-08-09 00:06:06.742
. . . .
. . . .
I don't understand why it thinks August is greater than
October. Can someone please elucidate what is going on?
Regards,
Tena Sakai
tsakai@gallo.ucsf.edu
Re: what's wrong with my date comparison?
On Tue, Oct 16, 2007 at 10:57:03AM -0700, Tena Sakai wrote: > select name, value, datecreated > from mytable > where datecreated > 2007-10-02; ^^^^^^^^^^ 2007-10-02 is an arithmetic expression equivalent to 1995. I think what you want is WHERE datecreated > '2007-10-02'; Note the quotes. A -- Andrew Sullivan | ajs@crankycanuck.ca In the future this spectacle of the middle classes shocking the avant- garde will probably become the textbook definition of Postmodernism. --Brad Holland
Re: what's wrong with my date comparison?
On Oct 16, 2007, at 12:57 , Tena Sakai wrote: > select name, value, datecreated > from mytable > where datecreated > 2007-10-02; where datecreated > '2007-10-02' 2007-10-02 = 1995. # select current_date < 2007-10-31 as arithmetic_comparison, current_date < '2007-10-31' as date_comparison; arithmetic_comparison | date_comparison -----------------------+----------------- f | t (1 row) Michael Glaesemann grzm seespotcode net
Re: what's wrong with my date comparison?
"Tena Sakai" writes: > I was missing quotes. > It must have evaluated 2007-10-02 and used it as a > numerical constant 1995. Actually, what you got was a *textual* comparison between '1995' and the timestamp converted to text, which makes even less sense. FWIW, as of PG 8.3 you'll get an error: regression=# select * from timestamp_tbl where d1 > 2007-10-02; ERROR: operator does not exist: timestamp without time zone > integer LINE 1: select * from timestamp_tbl where d1 > 2007-10-02; ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. We've seen too many people get burnt by variants of this problem... regards, tom lane