Skip to Main Content
  • Questions
  • Date Type Variable -- how to get DATE componet and TIME component.

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, chen.

Asked: June 06, 2000 - 8:06 pm UTC

Last updated: October 20, 2004 - 12:12 pm UTC

Version: Oracle 8.1.5

Viewed 10K+ times! This question is

You Asked

Dear sir,
How to separate a datetype varaible into date and time?
My database is oracle8.1.5,my ows is oracle web application server 3.0.2.

Thank you in advance!
John Chen

and Tom said...

To extract just the date component, you could:

select trunc(datecolumn) from T;

Trunc by default will removing the time component, you can trunc dates to many different levels -- hours, days, weeks, months, years and so on.

You could also just:

select to_char( datecolumn, 'dd-mon-yyyy' ) from t;

that is, use a date format to retrieve just the component of the date you were interested in.

As for the time component, it'll be very similar. For example:


select to_number(to_char(datecolumn,'sssss')) from T;

will return only the time component of the date as the number of seconds since midnight. Alternately, you may:

select to_number(to_char(datecolumn,'hh24')) hours,
to_number(to_char(datecolumn,'mi')) minutes,
to_number(to_char(datecolumn,'ss')) seconds
from T;

to get the hours, minutes, seconds in the time component in three fields.

There are many formats and functions you may apply to dates to perform these operations, the above are just a few and the most common. For more see

</code> http://docs.oracle.com/cd/F49540_01/DOC/server.815/a67779/function.htm#1028572 <code>

date functions and formats are documented there.

Rating

  (5 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

just what i needed

A reader, April 22, 2003 - 11:05 am UTC


only store the time component

A reader, October 19, 2004 - 5:46 pm UTC

Hi Tom,
We need to break a day into a few time blocks. So in the table we have columns "Date", "Start_Time", "End_Time". They all defined as date type. How can I store only time component in the time columns? I tried to_date(to_char('11:30','hh24:mi')), but Oracle default is as today's time, so added date information into the columns.

Thanks.

Tom Kyte
October 19, 2004 - 8:56 pm UTC

a date is a date -- period. 7 bytes -- always has a date (yyyymmdd hh24 mi ss) in it.

perhaps you want to use just

start_time timestamp,
duration interval,


the concept of storing the "day" in one field, a start time in another and an end in yet a third isn't really a good idea.

At MOST you need start_time, end_time both dates. that date column isn't useful.

only store the time component

A reader, October 19, 2004 - 6:04 pm UTC

Hi Tom,
Correction of the question I asked above. The three columns are "Day_of_Period", "Start_Time", "End_Time". It's to record pre-defined recursive events. I want only store time component in the start/end time columns but not the date component. Is make the columns varchar2 the only option?

Thank you!

Tom Kyte
October 19, 2004 - 8:57 pm UTC

do NOT store them in varchar2's.

they are dates, there should be TWO dates -- start and end, nothing more. this "day of period" thing isn't useful.


more clarifications

SRR, October 20, 2004 - 7:08 am UTC

Tom

Can you tell me how to store dates into fields which are in a format different from the format which is currently supported by a database...

thanks much...

Tom Kyte
October 20, 2004 - 11:17 am UTC

huh? does not compute. not sure what you mean.

only store the time component

A reader, October 20, 2004 - 12:05 pm UTC

I totally agree that date should be stored in date type field. But what we want is kind like a calendar template for recursive events, like in a 14 day period, every 2nd day from 9am to 10am is for group meeting, every 10th day from 2:30pm to 5:30pm, is for inventory check.... Since all events are recursive, it doesn't make sense to have date part in the fields. Of course programs can always just extract the time part, I just wonder if there is a way just store the time part in a date type field.

Thank you!

Tom Kyte
October 20, 2004 - 12:12 pm UTC

the date will always be there (just as the time is always there). just "ignore" the date component (like many just ignore the time). use the part you want.