4 Replies Latest reply on Sep 30, 2011 1:59 PM by 715267

    how to get a time part from Date datatype

    715267
      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
        • 1. Re: how to get a time part from Date datatype
          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.
          • 2. Re: how to get a time part from Date datatype
            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.
            • 3. Re: how to get a time part from Date datatype
              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
              • 4. Re: how to get a time part from Date datatype
                715267
                Hi,

                Thanks all for your inputs..!!:)