Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to get a time part from Date datatype

715267Sep 27 2011 — edited Sep 30 2011
Hi,

I would like to know how to extract the timestamp alone from DATE datatype? If my input is '27-SEP-2011 23:59:00' I need the output as 23:59:00. I am surprised to see that there are no in-built functions for this or may be I am wrong. Basically I need to remove the date part from DATE data type and get the time.Please assist.

-Thanks

Edited by: user9546145 on Sep 27, 2011 2:24 PM

Edited by: user9546145 on Sep 27, 2011 2:25 PM

Comments

Frank Kulash
Hi,
user9546145 wrote:
Hi,

I would like to know how to extract the timestamp alone from DATE datatype?
Be careful! In Oracle, TIMESTAMP means a datatype, similar to but distinct from DATE. You'll avoid confusion if you don't use the word "timestamp" to mean anything else.

There is a built-in function, TO_CHAR:
TO_CHAR (dt_col, 'HH24:MI:SS')
Depending on how you plan to use the time, TRUNC is another handy built-in function:
dt_col - TRUNC (dt_col)
is a NUMBER (not less than 0, but less than 1) which is suitable for many tasks, such as finding the average time.


 

I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
Hoek
TO_CHAR is your builtin function.
See http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/sql_elements004.htm#i34510
SQL> -- generating sample date:
SQL> with t as (
  2  select to_date('27-SEP-2011 23:59:00', 'dd-mon-yyyy hh24:mi:ss') dt from dual
  3  )
  4  --
  5  -- actual query:
  6  --
  7  select to_char(dt, 'hh24:mi:ss')
  8  from   t;

TO_CHAR(
--------
23:59:00

1 row selected.
bamboat_3
HI,

You may use to_date and to_char function together, it your parameter is text:

E.g: if you passing '27-SEP-2011 23:59:00' as a varchar2 datatype then first convert it into date and then extract the time from it; for that use this command:


SQL> select to_char(to_date('27-SEP-2011 23:59:00','dd-mm-yyyy HH24:MI:SS'),'HH24:MI:SS AM') time from dual;

TIME
--------
23:59:00

if you already passing the date type as parameter then just use to_char function for extract the time from it.
E.g:
Select to_char(sysdate,'HH24:MI:SS AM') from dual;

the AM will indicates that either its morning time or afternoon time automatically.

I hope this is your answer
Regards
M.A.Bamboat
715267
Hi,

Thanks all for your inputs..!!:)
1 - 4
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Oct 28 2011
Added on Sep 27 2011
4 comments
302,908 views