473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Comparing dates

My date setting is ISO with US conventions, and output from a select
is in the form yyyy-mm-dd (2002-01-18, for example.

When I do a select such as

SELECT * FROM table WHERE date BETWEEN '2001-12-28' AND '2002-01-28'

It misses the entry with date '2002-01-28' (which does exist!).

Likewise,

SELECT * FROM table WHERE date = '2001-12-28' ;

gives me '0 rows'.
The only way I've been able to handle this, so far, is
in this fashion:

SELECT * FROM table WHERE
date BETWEEN 20011228 - .001 AND 20020128 + .001 ;

and similarly instead of = using, I can use

date BETWEEN 20020128 - .001 AND 20020128 + .001
I must be missing something, but I can't find it.

What is the 'right' way to select for a date type = a particular
date, and for BETWEEN to work as advertised?

I have a copy of 'Practical Postgresql', but I can't find the
answer there, or in the online manual. Of course there are a
lot of places to look and I may have missed it.

Thanks,

John Velman
Nov 11 '05 #1
2 8668
On Tue, Sep 02, 2003 at 04:09:00PM -0700, John Velman wrote:
My date setting is ISO with US conventions, and output from a select
is in the form yyyy-mm-dd (2002-01-18, for example.

When I do a select such as

SELECT * FROM table WHERE date BETWEEN '2001-12-28' AND '2002-01-28'

It misses the entry with date '2002-01-28' (which does exist!).


So, your date column is actually a timestamp? Cast it to date first and
see if it works, e.g.

SELECT * FROM table WHERE date::date BETWEEN '2001-12-28' AND '2002-01-28'

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"La principal característica humana es la tontería"
(Augusto Monterroso)

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 11 '05 #2
"John Velman" <ve****@cox.net> writes:
When I do a select such as
SELECT * FROM table WHERE date BETWEEN '2001-12-28' AND '2002-01-28'
It misses the entry with date '2002-01-28' (which does exist!).
Uh, what exactly is the datatype of the "date" column?
The only way I've been able to handle this, so far, is
in this fashion:
SELECT * FROM table WHERE
date BETWEEN 20011228 - .001 AND 20020128 + .001 ;


I think the odds that this does what you think it does are vanishingly
small :-(. There's no date-vs-numeric comparison operator. It's
probably getting interpreted as some bizarre textual comparison.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 11 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: F | last post by:
Hi I have posted the question few days back about problem in inserting the dates in SQL server and thankful to them who replied. That was solved and this is a nice solution....
2
by: Mike N. | last post by:
I am currently using a function to validate a form on the client side (see code below). At the end of the function, I would like it to also compare a startDate against an endDate to ensure that the...
2
by: Duppypog | last post by:
I'm trying to compare a date stored in a database with today's date using an If statement, but it's not returning true. Example, value in database is 11/5/2003 with today being 11/6/2003. Can...
2
by: Philip Townsend | last post by:
I am having difficulty with a simple routine as follows: public static bool VerifyExpirationDate(DateTime date) { if(date>=DateTime.Now)return true; else return false; } The problem is that...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
2
by: ameshkin | last post by:
Hi GUys, Im trying to compare two dates in MYSQL. But its not treating the dates as numbers, but as strings. I try using strtotime but that did not work. Basically, if the last comment is...
12
by: colincolehour | last post by:
I am new to Python and am working on my first program. I am trying to compare a date I found on a website to todays date. The problem I have is the website only shows 3 letter month name and the...
4
by: cheryl | last post by:
I am using the PHP.MYSQL and Apache server application in developing my website. I have problem in comparing dates. Website has room reservation, the user will check first the room availability. The...
2
by: dantebothermy | last post by:
Is there a simple way to subtract the time from a datetime field, so when I compare dates, I'm always comparing the dates at 12:00 am? I'd really prefer not to convert all my dates to strings. ...
4
by: jupi13 | last post by:
i have this code..i don't know what where is the error in this one..it says data type mismatch..... Dim Mydate As Date Dim MydateString As String MydateString = "Text1.Text" Mydate =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.