简体   繁体   English

Oracle 将时间戳转换为日期

[英]Oracle convert Timestamp to Date

I have date like:我有这样的日期:

2020/05/09T02:40:03
2020/05/16T02:40:03
2020/05/15T02:40:03

I need to convert it into Date format 'dd/mm/yyyy'.我需要将其转换为日期格式“dd/mm/yyyy”。

My query我的查询

select cast(date_registered as date) as Reg_date from employee

Error:错误:

Literal does not match format string

Note:I have also used TO_Char , TO_Date but not working

First, what you tried to convert to date is not a timestamp , it is a string.首先,您尝试转换为date的不是timestamp ,而是字符串。

Second, cast(... to date) does not take a format model - it simply relies on your session nls_date_format parameter, which doesn't match the string's date format.其次, cast(... to date)不采用格式 model - 它仅依赖于您的 session nls_date_format参数,该参数与字符串的日期格式不匹配。 (Especially the boilerplate, hard-coded T in the middle, which has no meaning in Oracle.) (尤其是样板,中间硬编码 T,在 Oracle 中没有意义。)

You need to_date() with an appropriate format model.您需要使用适当格式 model 的to_date() Notice the handling of hardcoded string fragments (they appear in double-quotes in the format model).注意硬编码字符串片段的处理(它们出现在格式模型中的双引号中)。

select to_date(date_registered, 'yyyy/mm/dd"T"hh24:mi:ss') as reg_date 
from   employee;

In Oracle, a date data type is always stores as 7-bytes consisting of century, year-of-century, month, day, hours, minutes and seconds;在 Oracle 中,日期数据类型始终存储为 7 字节,包括世纪、世纪、月、日、小时、分钟和秒; so, asking to convert something that has year-to-seconds components to a date does not require you to do anything:因此,要求将具有年到秒组件的内容转换为日期不需要您做任何事情:

SELECT date_registered AS reg_date FROM employee

Now, if you are storing date_registered as a string data type rather than as a date data type then you need to convert using TO_DATE :现在,如果您将date_registered存储为字符串数据类型而不是日期数据类型,那么您需要使用TO_DATE进行转换:

SELECT TO_DATE( date_registered, 'YYYY-MM-DD"T"HH24:MI:SS' ) AS reg_date
FROM   employee

However, you should not do this and you should fix the underlying problem that you are storing dates as strings and not dates.但是,您不应该这样做,您应该解决将日期存储为字符串而不是日期的潜在问题。 You can solve this by converting the data type of the column:您可以通过转换列的数据类型来解决此问题:

ALTER TABLE employee ADD ( date_registered2 DATE );
UPDATE employee
  SET date_registered2 = TO_DATE( date_registered, 'YYYY-MM-DD"T"HH24:MI:SS' );
ALTER TABLE employee DROP COLUMN date_registered;
ALTER TABLE employee RENAME COLUMN date_registered2 TO date_registered;

(Note: The queries below assume that you have "fixed" the table so that the date_registered column has the date data type; if you do not want to do this then you will need to include the TO_DATE conversion as well.) (注意:下面的查询假定您已“修复”表,以便date_registered列具有日期数据类型;如果您不想这样做,那么您还需要包括TO_DATE转换。)

If you want the output to be a date where the time component is truncated back to midnight then:如果您希望 output 成为时间分量被截断回午夜的日期,则:

SELECT TRUNC( date_registered ) AS reg_date
FROM   employee

(Note: The displayed date will still have hours, minutes and seconds components but they will all be zero after being passed through TRUNC .) (注意:显示的日期仍然有小时、分钟和秒的组成部分,但在通过TRUNC后它们都将为零。)

If you want it in a specific format, without the time component, then you need to output it as a formatted string (since the date data type does not have an associated format) using TO_CHAR :如果您希望它采用特定格式,没有时间组件,那么您需要使用TO_CHAR将 output 作为格式化字符串(因为日期数据类型没有关联格式):

SELECT TO_CHAR( date_registered, 'DD/MM/YYYY' ) AS reg_date
FROM   employee

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

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

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