473,699 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

timestamp/date comparison

Hi,
I'm trying to compare a timestamp to current_timesta mp but I'm having
trouble.
I want to compare just the date piece of my timestamp column to just the
date piece of current_timesta mp.

I'm getting weird results that I don't understand.
When I use TO_DATE it changes the year, month, etc.

Any ideas?
anna=> SELECT request_date,TO _DATE(request_d ate,'YYYY-MM-DD HH24:MM:SS')
FROM anna_onestop_da tabase_t ; request_date | to_date
---------------------+------------
2003-10-08 09:15:57 | 2004-03-09
2003-10-08 09:18:37 | 2004-06-09
2003-10-08 09:20:11 | 2004-08-09
2003-10-08 09:27:56 | 2005-03-11
2003-10-08 09:30:38 | 2005-06-11
2003-10-08 09:32:09 | 2005-08-11
2003-10-08 09:47:07 | 2006-11-13
2003-10-08 10:04:22 | 2003-04-08
2003-10-08 11:46:37 | 2006-10-14
2003-10-08 12:08:41 | 2003-08-08
2003-10-08 12:21:52 | 2004-09-09
2003-10-08 12:42:27 | 2006-06-13
2003-10-08 13:13:53 | 2004-01-08

anna=> SELECT
TO_DATE(request _date,'MM/DD/YYYY'),TO_DATE( current_timesta mp,'MM/DD/YYYY
') FROM anna_onestop_da tabase_t WHERE TO_DATE(request _date,'MM/DD/YYYY')
TO_DATE(current _timestamp,'MM/DD/YYYY');

to_date | to_date
------------+------------
0181-10-03 | 0180-10-26
0181-10-03 | 0180-10-26
0181-10-03 | 0180-10-26
0181-10-03 | 0180-10-26
0181-10-03 | 0180-10-26
0181-10-03 | 0180-10-26
0181-10-03 | 0180-10-26
0181-10-03 | 0180-10-26
0181-10-03 | 0180-10-26
0181-10-03 | 0180-10-26
0181-10-03 | 0180-10-26
0181-10-03 | 0180-10-26
0181-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0182-10-03 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
0183-10-04 | 0180-10-26
thanks!

Troy Campano
Nov 22 '05 #1
2 21179
On Fri, 13 Feb 2004, Campano, Troy wrote:
Hi,
I'm trying to compare a timestamp to current_timesta mp but I'm having
trouble.
I want to compare just the date piece of my timestamp column to just the
date piece of current_timesta mp.

I'm getting weird results that I don't understand.
When I use TO_DATE it changes the year, month, etc.
I think you probably don't want to_date in any case. CAST(whatever AS
DATE) is probably better. The to_date way probably is taking the
timestamp converting it to text and then attempting to convert the text
back.
anna=> SELECT request_date,TO _DATE(request_d ate,'YYYY-MM-DD HH24:MM:SS')
Here, you're using the minutes as month information I think.
anna=> SELECT
TO_DATE(request _date,'MM/DD/YYYY'),TO_DATE( current_timesta mp,'MM/DD/YYYY
') FROM anna_onestop_da tabase_t WHERE TO_DATE(request _date,'MM/DD/YYYY')
TO_DATE(current _timestamp,'MM/DD/YYYY');


I'm not sure why this is working at all, but using the standard output
format for a timestamp, it doesn't follow the form MM/DD/YYYY I believe,
so the format string doesn't really line up with the data.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 22 '05 #2
"Campano, Troy" <Tr**********@L ibertyMutual.co m> writes:
anna> SELECT request_date,TO _DATE(request_d ate,'YYYY-MM-DD HH24:MM:SS')


The correct way to write the format string would have been

SELECT request_date,TO _DATE(request_d ate,'YYYY-MM-DD HH24:MI:SS')

(minutes are MI not MM). It was evidently taking the minute number as
month number, and not noticing that the field was out of range :-(.
People have complained before that to_date() and related functions don't
detect all the error cases one would reasonably expect them to complain
about ...

However, this all seems like the hard way to solve your problem.
Why don't you just cast the timestamp value to date type, ie
"CAST(request_d ate AS date)", or just "request_date:: date" if you don't
mind using a Postgres-specific syntax. The date_trunc() function also
is worth knowing about.

regards, tom lane

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

Nov 22 '05 #3

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

Similar topics

2
32422
by: Ben | last post by:
I would like to use php to query a database and retrieve a unix timestamp. The problem is that mysql is storing the data in the date format and not a timestamp. I am sure that I can amend my query to format the date returned as a timestamp without having to do the conversion in php. Can someone tell me what to put in my db query?
2
11645
by: Phil Powell | last post by:
I am forced to have to compare the value of some PHP timestamp with a Java-based java.util.Date.getTime() timestamp. Let me show you how they look: The Java time is returned to the PHP class method as mixed but could be converted to float I would think, but how would I ensure that they
13
9293
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and three dropdow boxes for hours, minutes, and AM/PM. All of these need to be considered together and converted to one Unix Timestamp and then inserted to the MYSQL date field. The type of field is INT (11) so that I can instead of the standard...
6
3428
by: Jim C. Nasby | last post by:
Is there any reason why there isn't a predefined cast to go from a timestamp to a varchar? Is there a reason not to add one? -- Jim C. Nasby, Database Consultant jim@nasby.net Member: Triangle Fraternity, Sports Car Club of America Give your computer some brain candy! www.distributed.net Team #1828 Windows: "Where do you want to go today?" Linux: "Where do you want to go tomorrow?" FreeBSD: "Are you guys coming, or...
2
4351
by: Russell Smith | last post by:
Timestamps support infinity. However if appears dates do not. When timestamps are cast to dates, there is no output. Is this an acceptable option or not? Below are a number of examples showing what I am experiencing. The last own shows how converting timestamps to dates and then ordering doesn't give you the order you want. Maybe you should just order by the timestamp to begin with. However Date does not understand infinity at all.
6
6310
by: Scott Nixon | last post by:
New to Postgres 7.3 from 7.0. Am having some trouble with a query that worked in 7.0 but not in 7.3.....can't seem to figure out the syntax or find info about how to do this anywhere. Consider for the following query: - 'number' is an integer - 'procedures' is the table name - 'date' is a timestamp
7
6110
by: JJ | last post by:
How do I set one field to have the updated timestamp, and another to have the created timestamp? I want to do this directly from code generated from DB Designer if possible?! JJ
8
2071
by: kanwal | last post by:
Hi, I have millions of records in my xxxxx table in mysql. And I have a column of time in which I have stored the timestamp using php time() function. Now I wanna write an SQL query to fetch the records either for year (2006) or for month and year (Jan 2006) Currently I had implement this logic:
0
1924
gregerly
by: gregerly | last post by:
Hello, I've got a question regarding the comparison of a date and year to a timestamp. I've got blog entries that get a timestamp on their way into my database (mysql). I'm programming an "archives" section, and I can't seem to select the info using month and year. Basically what I want to do is say: select all artices where the month of the timestamp = $given_month AND year of the timestamp = $given_year. To accomplish the above I...
0
9199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9055
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8902
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7787
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4641
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3075
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.