dbTalk Databases Forums  

Oracle Timestamp

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Oracle Timestamp in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Faby
 
Posts: n/a

Default Oracle Timestamp - 08-10-2006 , 02:58 AM






Hi,
I was wondering how I should convert a timestamp to compare it to
'01-AUG-2006'.

Is any of the following right?

TO_CHAR(TRUNC(MYTIMESTAMP)) < '01-AUG-2006'

TO_DATE(TRUNC(MYTIMESTAMP)) < '01-AUG-2006'

TO_CHAR(MYTIMESTAMP,'DD-MON-YYYY') < '01-AUG-2006'

If you could give a brief explaination of why I am getting a different
result for each of the previous answer, i would really appreciate.

Thank you
Faby


Reply With Quote
  #2  
Old   
Martin T.
 
Posts: n/a

Default Re: Oracle Timestamp - 08-10-2006 , 04:56 AM






Faby wrote:
Quote:
Hi,
I was wondering how I should convert a timestamp to compare it to
'01-AUG-2006'.

Is any of the following right?

TO_CHAR(TRUNC(MYTIMESTAMP)) < '01-AUG-2006'

TO_DATE(TRUNC(MYTIMESTAMP)) < '01-AUG-2006'

TO_CHAR(MYTIMESTAMP,'DD-MON-YYYY') < '01-AUG-2006'

If you could give a brief explaination of why I am getting a different
result for each of the previous answer, i would really appreciate.

If you want to compare timestamps, compare timestamps, not strings.

TRUNC(MYTIMESTAMP) < TO_DATE('01-AUG-2006', fmt_string)

Btw. Do you have TIMESTAMPs, or DATEs? (Though I guess it doesn't make
a difference in this case.)

best,
Martin



Reply With Quote
  #3  
Old   
Faby
 
Posts: n/a

Default Re: Oracle Timestamp - 08-10-2006 , 05:03 AM



Hi Martin,
Thank you for your reply. I need to convert my timestamp to a string as
i'd like to compare it to '01-AUG-2006'. I thought that
TO_CHAR(MYTIMESTAMP,'DD-MON-YYYY') < '01-AUG-2006' would do it, but
someone else from work told me to use
TO_DATE(SUBSTR(TO_CHAR(MYTIMESTAMP), 1, 9), 'DD-MON-RRRR') <
'01-AUG-2006' but i'm sure if he's right or not. Could you explain it,
please?
Thanks
Faby


Reply With Quote
  #4  
Old   
Martin T.
 
Posts: n/a

Default Re: Oracle Timestamp - 08-10-2006 , 05:17 AM



Faby wrote:
Quote:
Hi Martin,
Thank you for your reply. I need to convert my timestamp to a string as
i'd like to compare it to '01-AUG-2006'. I thought that
TO_CHAR(MYTIMESTAMP,'DD-MON-YYYY') < '01-AUG-2006' would do it, but
someone else from work told me to use
TO_DATE(SUBSTR(TO_CHAR(MYTIMESTAMP), 1, 9), 'DD-MON-RRRR')
'01-AUG-2006' but i'm sure if he's right or not. Could you explain it,
please?

Why would you want to compare the date as a string?
You want to know if your timestamp is before the 1st of August 2006,
right?
Then use:
TRUNC(MYTIMESTAMP) < TO_DATE('01-AUG-2006', fmt_string)

Quote:
TO_CHAR(MYTIMESTAMP,'DD-MON-YYYY') < '01-AUG-2006'

This will compare strings, and is most likely not what you want!
'01-AUG-2006' < '01-JAN-2001' < '02-MAY-0000' - for STRINGS

Quote:
TO_DATE(SUBSTR(TO_CHAR(MYTIMESTAMP), 1, 9), 'DD-MON-RRRR')
'01-AUG-2006' but i'm sure if he's right or not. Could you explain it,

This does make even less sense to me ...

best,
Martin



Reply With Quote
  #5  
Old   
Mark D Powell
 
Posts: n/a

Default Re: Oracle Timestamp - 08-10-2006 , 11:41 AM




Martin T. wrote:
Quote:
Faby wrote:
Hi Martin,
Thank you for your reply. I need to convert my timestamp to a string as
i'd like to compare it to '01-AUG-2006'. I thought that
TO_CHAR(MYTIMESTAMP,'DD-MON-YYYY') < '01-AUG-2006' would do it, but
someone else from work told me to use
TO_DATE(SUBSTR(TO_CHAR(MYTIMESTAMP), 1, 9), 'DD-MON-RRRR')
'01-AUG-2006' but i'm sure if he's right or not. Could you explain it,
please?

Why would you want to compare the date as a string?
You want to know if your timestamp is before the 1st of August 2006,
right?
Then use:
TRUNC(MYTIMESTAMP) < TO_DATE('01-AUG-2006', fmt_string)

TO_CHAR(MYTIMESTAMP,'DD-MON-YYYY') < '01-AUG-2006'

This will compare strings, and is most likely not what you want!
'01-AUG-2006' < '01-JAN-2001' < '02-MAY-0000' - for STRINGS

TO_DATE(SUBSTR(TO_CHAR(MYTIMESTAMP), 1, 9), 'DD-MON-RRRR')
'01-AUG-2006' but i'm sure if he's right or not. Could you explain it,

This does make even less sense to me ...

best,
Martin
Faby, convert the date string to a timestamp and compare timestamps:

UT1 > select to_timestamp('01-AUG-06')
2 from sys.dual;

TO_TIMESTAMP('01-AUG-06')
---------------------------------------------------------------------------
01-AUG-06 12.00.00.000000000 AM


If your column values have a time component then use >= and <
conditions to ignore the time portion of the column value.

UT1 > l
1 select to_timestamp('01-AUG-06') as BeginTime,
2 to_timestamp(to_date('01-AUG-06') + 1) as EndTime
3* from sys.dual
UT1 > /

BEGINTIME
---------------------------------------------------------------------------
ENDTIME
---------------------------------------------------------------------------
01-AUG-06 12.00.00.000000000 AM
02-AUG-06 12.00.00 AM

HTH -- Mark D Powell --



Reply With Quote
  #6  
Old   
Faby
 
Posts: n/a

Default Re: Oracle Timestamp - 08-11-2006 , 02:56 AM



Hi all,
Thank you very much for your answers. I have now a better understanding
of the timestamp data type.:-)
I haven't been able to convince anyone from work to use any of your
ideas, though i'm sure that my next scripts will hold one of your
version. It really makes more sense to me.
Thanks again
Faby


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.