Convert timestamp to date in Oracle SQL

前端 未结 7 2138
花落未央
花落未央 2020-12-05 23:17

How can we convert timestamp to date?

The table has a field, start_ts which is of the timestamp format:

\'05/13/2016 4:58:1         


        
相关标签:
7条回答
  • 2020-12-05 23:41

    If the datatype is timestamp then the visible format is irrelevant.

    You should avoid converting the data to date or use of to_char. Instead compare the timestamp data to timestamp values using TO_TIMESTAMP()

    WHERE start_ts >= TO_TIMESTAMP('2016-05-13', 'YYYY-MM-DD')
       AND start_ts < TO_TIMESTAMP('2016-05-14', 'YYYY-MM-DD')
    
    0 讨论(0)
  • 2020-12-05 23:41

    You can use:

    select to_date(to_char(date_field,'dd/mm/yyyy')) from table
    
    0 讨论(0)
  • 2020-12-05 23:43

    This may not be the correct way to do it. But I have solved the problem using substring function.

    Select max(start_ts), min(start_ts)from db where SUBSTR(start_ts, 0,9) ='13-may-2016'
    

    using this I was able to retrieve the max and min timestamp.

    0 讨论(0)
  • 2020-12-05 23:57
    CAST(timestamp_expression AS DATE)
    

    For example, The query is : SELECT CAST(SYSTIMESTAMP AS DATE) FROM dual;

    0 讨论(0)
  • 2020-12-06 00:00

    Format like this while selecting:

    to_char(systimestamp, 'DD-MON-YYYY')
    

    Eg:

    select to_char(systimestamp, 'DD-MON-YYYY') from dual;
    
    0 讨论(0)
  • 2020-12-06 00:03

    Try using TRUNC and TO_DATE instead

    WHERE
        TRUNC(start_ts) = TO_DATE('2016-05-13', 'YYYY-MM-DD')
    

    Alternatively, you can use >= and < instead to avoid use of function in the start_ts column:

    WHERE
       start_ts >= TO_DATE('2016-05-13', 'YYYY-MM-DD')
       AND start_ts < TO_DATE('2016-05-14', 'YYYY-MM-DD')
    
    0 讨论(0)
提交回复
热议问题