从oracle tmstmp字段中删除毫秒

时间:2014-12-26 15:43:04

标签: sql oracle oracle11g

我在Oracle中有一个TIMESTAMP(6)字段,我需要从那时起删除毫秒组件。

例如我有

10/20/2014 10:34:06.356000 AM

我想删除毫秒,以便我有

10/20/2014 10:34:06 AM

你知道最好的方法吗?

谢谢!

4 个答案:

答案 0 :(得分:3)

这个怎么样?

select cast(col as timestamp(0))

编辑:

避免舍入的最简单方法是使用trunc()或减去半秒:

select cast(col - 0.5/(24*60*60) as timestamp(0))

答案 1 :(得分:1)

试试这个

SELECT TO_CHAR(SYSDATE, 'MM-DD-YYYY HH24:MI:SS') "NOW"
FROM DUAL;

如果您需要12小时的日期格式

SELECT TO_CHAR(SYSDATE, 'MM-DD-YYYY HH:MI:SS AM') "NOW"
FROM DUAL;

SQL FIDDLE

答案 2 :(得分:0)

您可以将其投射到没有小数秒的时间戳(这将舍入到最接近的秒数):

def test_0_optional_args_works():
        test_example = Example(im_1, im_2)
        assert test_example.im_1 = im_1
        assert test_example.im_2 = im_2
        assert test_example.im_3 = None
        assert test_example.im_4 = None
        assert test_example.value = im_1-im_2

def test_2_optional_args_works():
        test_example = Example(im_1, im_2, im_3, im_4)
        assert test_example.im_1 = im_1
        assert test_example.im_2 = im_2
        assert test_example.im_3 = im_3
        assert test_example.im_4 = im_4
        assert test_example.value = (im_1-im_2)/(im_3-im_4)

CAST( your_timestamp AS TIMESTAMP(0) ) 数据类型(这将截断到最接近的秒数):

DATE

如果您想将其作为CAST( your_timestamp AS DATE ) 数据类型,请将其转换回来:

TIMESTAMP(0)

或者您可以将其转换为格式化字符串并指定要使用的格式模型(这将截断到最接近的秒):

CAST( CAST( your_timestamp AS DATE ) AS TIMESTAMP(0) )

像这样:

SQL Fiddle

Oracle 11g R2架构设置

TO_CHAR( your_timestamp, 'YYYY-MM-DD HH24:MI:SS' )

查询1

CREATE TABLE your_table ( your_timestamp ) AS
  SELECT TIMESTAMP '2017-10-25 12:53:12.10076' FROM DUAL;

<强> Results

SELECT CAST( your_timestamp AS TIMESTAMP(0) ) AS "Timestamp",
       CAST( your_timestamp AS DATE ) AS "Date",
       TO_CHAR( your_timestamp, 'DD-MM-YYYY HH24:MI:SS' ) AS "String"
FROM   your_table

注意:| Timestamp | Date | String | |-----------------------|----------------------|---------------------| | 2017-10-25 12:53:12.0 | 2017-10-25T12:53:12Z | 25-10-2017 12:53:12 | TIMESTAMP在输出中的格式取决于您的DATENLS_TIMESTAMP_FORMAT会话参数,但您可以直接控制{NLS_DATE_FORMAT格式1}}指定格式模型时。

答案 3 :(得分:-2)

这可能会有所帮助!

select substr(to_char('10/20/2014 10:34:06.356000 AM'),1,instr(to_char('10/20/2014 10:34:06.356000 AM'),'.')-1)||' '||
substr(to_char('10/20/2014 10:34:06.356000 AM'),-2,instr(to_char('10/20/2014 10:34:06.356000 AM'),'.')-1) "Date"
from dual;