Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Comparison giving false results

Status
Not open for further replies.

jwalacha

Programmer
Aug 2, 2002
33
US
Hi,

I am getting incorrect results for my select statement when querying the table with the following information:

Timestamp ID
-------------------------------
08/03/02 03:06:18 100
08/03/02 03:06:59 101
08/03/02 03:16:57 226
08/04/02 12:12:12 227

The query that I am using is:

select ID from TABLE1
where TIMESTAMP > to_date('08/03/02 03:16:57','mm/dd/yy hh24:mi:ss');

I would expect to get the result of ID = 227
But it does not give me the expected result.

I appreciate your help.

Thanks,

Jwalant.

 
Try running this statement to double check your centuries:
Code:
select ID,
       to_char(TIMESTAMP,'mm/dd/yyyy hh24:mi:ss')
       full_date_time
from TABLE1
order by TIMESTAMP;
Maybe this will help you see the problem.


Code:
select * from Life where Brain is not null
Consultant/Custom Forms & PL/SQL - Oracle 8.1.7 - Windows 2000
[sup]When posting code, please use TGML for readability. Thanks![sup]
 
BJCooperIT,

I ran your code and the value of year was 2002, which meant that I was in the correct century.

Although I change my select statement from 'yy' to 'yyyy' format to eliminate any doubts and the query result was correct.
Thank you for your response.

I'd appreciate if you can answer my next question:

Please refer to the table contents in the above email.

The following query...

select ID from TABLE1
where TIMESTAMP > to_date('08/03/02 03:06:59','mm/dd/yy hh24:mi:ss');

would give me two (2) records, ID = 226 and 227 respectively.

Is there a quick way to write a select statement so that I only get the record that has the ID for the timestamp right after 8/3/02 03:06:59, namely ID 226 ?

I hope I was clear in stating my question.

Thanks,

Jwalant.
 
Now that I understand your question a bit better...
I cannot get an SQL statement to do this on my operating system. My solution would be to create a function that would contain a cursor that would read the first record dated after your desired date and return its rowid. Your SQL would look something like:
Code:
select ID from TABLE1
where  rowid = F_GET_TABLE1_ROWID_AFTER_DATE
  (to_date('20020803030659','YYYYMMDDHH24MISS'));
Perhaps someone else can offer an SQL statement using an inline view with rownum that will work on your system.

Good Luck!

Code:
select * from Life where Brain is not null
Consultant/Custom Forms & PL/SQL - Oracle 8.1.7 - Windows 2000
[sup]When posting code, please use TGML for readability. Thanks![sup]
 
try this:

select *
from (
select a.*,
row_number() over ( order by timestamp desc)
rn
from table1 a
where TIMESTAMP >to_date('08/03/02 03:06:59','mm/dd/yy hh24:mi:ss'))
where rn=1
 
If you work with recent dates, you may use 'RR' mask instead of 'YY'. This will treat '02' as '2002' and '50' as '1950'.

Regards, Dima
 
Thank you Manjula,
That was exactly what I was looking for!

I appreciate everybody's input in helping me with a solution.

Regards,

Jwalant.
 
Hi, Just a nitpick..Why use RR ?
Typing 2 more chars ( YYYY ) will insure century correctness..( Until the Y10K problem surfaces)
Just my 2c

[profile]
 
Turkbear: can you explain the same to our customers? :)

Regards, Dima
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top