[英]Converting timestamp to date in Oracle 12c
I want to convert a given timestamp in such format: 2019-04-08 00:00:00.0
to a date in this format: 2019-04-08
. 我想将以下格式的给定时间戳记: 2019-04-08 00:00:00.0
转换为以下格式的日期: 2019-04-08
。
I have already tried using: 我已经尝试使用:
SELECT TO_DATE('2019-04-20 00:00:00.0','YYYY-MM-DD') from dual;
But I got prompted with: 但是我得到提示:
ORA-01830: date format picture ends before converting entire input string ORA-01830:日期格式图片在转换整个输入字符串之前结束
I think you may have some conceptual misunderstanding about how the TO_DATE function works, and also about how dates are processed by the DBMS. 我认为您可能对TO_DATE函数的工作原理以及DBMS如何处理日期有一些概念上的误解。
YYY-MM-DD
does not match the format of the actual string you're importing ( 2019-04-20 00:00:00.0
) That's what the error is telling you. YYY-MM-DD
与您要导入的实际字符串的格式不匹配( 2019-04-20 00:00:00.0
)这就是错误告诉您的内容。 You must tell the TO_DATE function what to expect in the date string you input into it. 您必须告诉TO_DATE函数在输入的日期字符串中期望什么。 You do that by means of the format string. 您可以通过格式字符串来实现。 if you don't specify a format string which matches the format you're actually going to supply, then the function will fail to process the string. 如果您没有指定与实际要提供的格式匹配的格式字符串,则该函数将无法处理该字符串。
Next, you say you want to convert it "to a date in this format"...but this does not entirely make sense. 接下来,您说您想将其“转换为这种格式的日期” ...但这并不完全有意义。 TO_DATE converts a string into a variable of type DATETIME
- ie a date object. TO_DATE将字符串转换为DATETIME
类型的变量-即日期对象。 A date object does not not exist in any particular format, it exists as an object. 日期对象不存在任何特定格式,而是作为对象存在。 Internally it will store the date information in a way which is independent of any human-readable date format. 在内部,它将以独立于任何人类可读日期格式的方式存储日期信息。 The format relates entirely to the presentation of the date when seen as a string . 格式完全与日期显示有关(当显示为字符串时) 。 Once you have a date object, you can then output the date in a particular format if you want to a human to be able to read it in the style that their culture is familiar with. 拥有日期对象后,如果您希望人类能够以其文化所熟悉的风格阅读日期,则可以以特定格式输出日期。
So, firstly to import your date string correctly as a date object, you can use an accurate format string, an also use TO_TIMESTAMP instead of TO_DATE so that it captures the sub-seconds value: 因此,首先要正确地将日期字符串作为日期对象导入,可以使用准确的格式字符串,也可以使用TO_TIMESTAMP而不是TO_DATE,以便捕获亚秒值:
SELECT TO_TIMESTAMP('2019-04-20 00:00:00.0','YYYY-MM-DD HH24:MI:SS.FF5') from dual;
If you run this in a console the SELECT will then automatically re-format that date object (the result of the TO_DATE function) into the default date format configured in your server / session. 如果您在控制台中运行此命令,那么SELECT将自动将该日期对象(TO_DATE函数的结果)重新格式化为服务器/会话中配置的默认日期格式。
However if you actually want to see it on screen in a particular format, you can explicitly say so - a sensible way is using the TO_CHAR function: 但是,如果您实际上想在屏幕上以某种特定格式查看它,则可以明确地这样说-一种明智的方法是使用TO_CHAR函数:
SELECT TO_CHAR(TO_TIMESTAMPT('2019-04-20 00:00:00.0','YYYY-MM-DD HH24:MI:SS.FF5'), 'YYYY-MM-DD') from dual;
The full list of format specifiers can be found here (and in other places online as well). 格式说明符的完整列表可以在此处 (以及在线的其他地方)中找到。
Live demo of the above here: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=619d918ea73953e11b3150c6b560112c 此处的实时演示: https : //dbfiddle.uk/?rdbms=oracle_18&fiddle=619d918ea73953e11b3150c6b560112c
Assuming the input is actual text, and not a real timestamp, you could try just truncating the text before you call TO_DATE
: 假设输入的是实际文本,而不是真实的时间戳,则可以在调用TO_DATE
之前尝试截断文本:
WITH cte AS (
SELECT '2019-04-20 00:00:00.0' AS ts FROM dual
)
SELECT TO_DATE(SUBSTR(ts, 1, 10), 'YYYY-MM-DD')
FROM cte;
If your input is an actual Oracle timestamp, and you want to convert it to a date, then you may use CAST
: 如果您输入的是实际的Oracle时间戳,并且要将其转换为日期,则可以使用CAST
:
SELECT CAST(ts AS DATE) dt
FROM cte;
Would CAST do any good? CAST有什么好处吗?
I'm setting date format so that it displays time component, although it is 00:00: 我正在设置日期格式,以使其显示时间成分,尽管它是00:00:
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select cast(timestamp '2019-04-20 00:00:00.0' as date) result from dual;
RESULT
-------------------
20.04.2019 00:00:00
Another format (without time component): 另一种格式(没有时间成分):
SQL> alter session set nls_date_format = 'dd.mm.yyyy';
Session altered.
SQL> select cast(timestamp '2019-04-20 00:00:00.0' as date) result from dual;
RESULT
----------
20.04.2019
SQL>
Or, using TO_CHAR
function (so that session's date format doesn't matter): 或者,使用TO_CHAR
函数(因此会话的日期格式无关紧要):
SQL> select to_char(timestamp '2019-04-20 00:00:00.0', 'dd.mm.yyyy') result from dual;
RESULT
----------
20.04.2019
SQL>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.