简体   繁体   English

无法在 oracle sql 中将时间戳转换为字符或日期

[英]Not able to convert timestamp to char or date in oracle sql

I need to convert timestamp value(19-01-21 09:15:00.000000 PM) to date format('YYYY/MM/DD HH24:MI:SS').我需要将时间戳值(19-01-21 09:15:00.000000 PM)转换为日期格式('YYYY/MM/DD HH24:MI:SS')。 I'm trying to do it using to_char function (to convert it to varchar and use it in TO_DATE function).我正在尝试使用 to_char function 来完成它(将其转换为 varchar 并在 TO_DATE 函数中使用它)。 I'm getting below error in to_char function.我在 to_char function 中遇到错误。 Is this the feasible way or please suggest if anything better works这是可行的方法还是请建议是否有更好的方法

select TO_CHAR('19-01-21 09:15:00.000000 PM','DD-MM-YY HH:MI:SSxFF AM') from dual; 
select TO_DATE(TO_CHAR('19-01-21 09:15:00.000000 PM','DD-MM-YYYY HH:MI:SSxFF AM'),'YYYY/MM/DD HH24:MI:SS'  ) from dual;

I'm getting below error:我收到以下错误:

ORA-01722: invalid number
01722. 00000 -  "invalid number"
*Cause:    The specified number was invalid.
*Action:   Specify a valid number.

'19-01-21 09:15:00.000000 PM' is a string literal; '19-01-21 09:15:00.000000 PM'是字符串文字; it is not a DATE or TIMESTAMP data type.它不是DATETIMESTAMP数据类型。

Since your string has fractional seconds, you can use TO_TIMESTAMP to convert it to a TIMESTAMP data type:由于您的字符串有小数秒,您可以使用TO_TIMESTAMP将其转换为TIMESTAMP数据类型:

SELECT TO_TIMESTAMP( '19-01-21 09:15:00.000000 PM', 'DD-MM-RR HH12:MI:SS.FF6 AM' )
         AS timestamp_value
FROM   DUAL;

Which outputs:哪个输出:

 | | TIMESTAMP_VALUE | TIMESTAMP_VALUE | |:---------------------------- | |:---------------------------- | | | 2021-01-19 21:15:00.000000000 | 2021-01-19 21:15:00.000000000 |

If you want the value as a DATE data type then you can take the previous output and CAST it to a DATE :如果您希望将该值作为DATE数据类型,那么您可以采用之前的 output 并将其CASTDATE

SELECT CAST(
         TO_TIMESTAMP( '19-01-21 09:15:00.000000 PM', 'DD-MM-RR HH12:MI:SS.FF6 AM' )
         AS DATE
       ) AS date_value
FROM   DUAL;

or, if the fractional seconds are always going to be zero:或者,如果小数秒总是为零:

SELECT TO_DATE( '19-01-21 09:15:00.000000 PM', 'DD-MM-RR HH12:MI:SS".000000" AM' )
         AS date_value
FROM   DUAL;

Which both output:其中output:

 | | DATE_VALUE | DATE_VALUE | |:------------------ | |:----------------- | | | 2021-01-19 21:15:00 | 2021-01-19 21:15:00 |

db<>fiddle here db<> 在这里摆弄


I need to convert timestamp value to date / varchar value.我需要将时间戳值转换为date / varchar值。

If you want to format the value as YYYY/MM/DD HH24:MI:SS then you can use TO_TIMESTAMP and then TO_CHAR :如果要将值格式化为YYYY/MM/DD HH24:MI:SS则可以使用TO_TIMESTAMP然后TO_CHAR

SELECT TO_CHAR(
         TO_TIMESTAMP( '19-01-21 09:15:00.000000 PM', 'DD-MM-RR HH12:MI:SS.FF6 AM' ),
         'YYYY/MM/DD HH24:MI:SS'
       ) AS date_string
FROM   DUAL;

Which outputs:哪个输出:

 | | DATE_STRING | DATE_STRING | |:------------------ | |:----------------- | | | 2021/01/19 21:15:00 | 2021/01/19 21:15:00 |

(Note: this outputs a string data type and not a DATE data type; if you want a DATE data type then use the 2nd or 3rd example.) (注意:这会输出字符串数据类型而不是DATE数据类型;如果您想要DATE数据类型,请使用第二个或第三个示例。)

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM