Forum Stats

  • 3,838,691 Users
  • 2,262,394 Discussions
  • 7,900,734 Comments

Discussions

how to get a time part from Date datatype

715267
715267 Member Posts: 9
edited Sep 30, 2011 9:59AM in SQL & PL/SQL
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
3179831

Answers

  • Frank Kulash
    Frank Kulash Member, Moderator Posts: 42,246 Red Diamond
    edited Sep 27, 2011 5:46PM
    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
    Hoek Member Posts: 16,087 Gold Crown
    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
    bamboat_3 Member Posts: 12
    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
    3179831
  • 715267
    715267 Member Posts: 9
    Hi,

    Thanks all for your inputs..!!:)
This discussion has been closed.