[英]How to convert Mon Dec 21 00:00:00 EST 2020 into dd/mm/yyyy in oracle sql
How we can convert "Mon Dec 21 00:00:00 EST 2020" to dd/mm/yyyy in oracle SQL?我们如何在 oracle SQL 中将“Mon Dec 21 00:00:00 EST 2020”转换为 dd/mm/yyyy? The date is stored in the datatype NVARCHAR2.
日期存储在数据类型 NVARCHAR2 中。
Here is how you can accept the input as an nvarchar2
and give the output as a varchar2
(you can cast it as nvarchar2
if you need that for some reason - see how I did that for the input).这是您如何接受输入作为
nvarchar2
并将 output 作为varchar2
的方法(如果出于某种原因需要,您可以将其转换为nvarchar2
- 看看我是如何为输入做的)。
In most cases, you would only need the timestamp with time zone for further computations - converting back to a string should be only the last step, when you prepare the results for a human-readable report.在大多数情况下,您只需要带有时区的时间戳来进行进一步的计算 - 当您为可读报告准备结果时,转换回字符串应该只是最后一步。 If you need the "result" in further computations, just strip away the outer call to
to_char
.如果您在进一步计算中需要“结果”,只需去掉对
to_char
的外部调用。
The third line of code shows the input .第三行代码显示了输入。 I don't have your data, so I created it from scratch - it is an
nvarchar2
string representing a timestamp with time zone, in the format you showed us.我没有您的数据,所以我从头开始创建它 - 它是一个
nvarchar2
字符串,表示带有时区的时间戳,采用您向我们展示的格式。 Notice the specification of language within the call to to_timestamp_tz
;注意
to_timestamp_tz
调用中的语言规范; without it, if your default is something other than English, "Mon" and "Dec" will cause an error to be thrown.没有它,如果您的默认值不是英语,“Mon”和“Dec”将导致抛出错误。
select to_char(
to_timestamp_tz(
to_nchar('Mon Dec 21 00:00:00 EST 2020'),
'Dy Mon dd hh24:mi:ss TZR yyyy',
'nls_date_language = english'),
'dd/mm/yyyy') as result
from dual
;
RESULT
----------
21/12/2020
EDIT编辑
In further comments, the OP asked about different input strings, which have 'EDT'
as the time zone component.在进一步的评论中,OP 询问了不同的输入字符串,这些字符串将
'EDT'
作为时区组件。
That is a problem.这是一个问题。 EST is not ambiguous, but EDT is, since different time zone regions use the EDT daylight saving time marker, and they don't all change to daylight saving time at the same time every year.
EST 并不模棱两可,但 EDT 是模棱两可的,因为不同的时区区域使用 EDT 夏令时标记,并且它们不会在每年的同一时间全部更改为夏令时。 Unlike EST, EDT must appear with an actual time zone region name in the input;
与 EST 不同,EDT必须与输入中的实际时区区域名称一起出现; EDT is really not a time zone region, it is only a DST marker for different regions, and it can't be used by itself to identify the correct region.
EDT 真的不是时区区域,它只是针对不同区域的 DST 标记,不能单独使用它来识别正确的区域。
The OP stated that all inputs are supposed to be in the 'America/New_York'
region. OP 声明所有输入都应该在
'America/New_York'
地区。 If so, then that can be added (concatenated) to the inputs, and the format model can be changed to add the TZR
element, as shown below.如果是这样,则可以将其添加(连接)到输入中,并且可以更改格式 model 以添加
TZR
元素,如下所示。 I only show the part where we convert from string to timestamp with time zone;我只展示了我们从字符串转换为带有时区的时间戳的部分; the rest doesn't need to change.
rest 不需要更改。
Note - ts
in the result of the query is of data type timestamp with time zone
;注意 - 查询结果中的
ts
是数据类型timestamp with time zone
; it looks as strings (in a specific format) in the output, because the output always shows strings, but in fact the result is the correct data type for further processing, if needed - for example, if one must select only the rows where the timestamp is in 2021, etc. In particular, one can apply to_char
to the result, with the `dd/mm/yyyy' format model, to match the OP's original request.它在 output 中看起来像字符串(以特定格式),因为 output 始终显示字符串,但实际上如果需要,结果是正确的数据类型以供进一步处理 - 例如,如果必须 Z99938282E1407184EFFZ1时间戳在 2021 年等。特别是,可以将
to_char
应用于结果,使用 `dd/mm/yyyy' 格式 model,以匹配 OP 的原始请求。
with
inputs (ts) as (
select 'Mon Dec 21 00:00:00 EST 2020' from dual union all
select 'Thu Mar 18 00:00:00 EDT 2021' from dual
)
select to_timestamp_tz(ts || ' America/New_York',
'Dy Mon dd hh24:mi:ss TZD yyyy TZR',
'nls_date_language = english') as ts
from inputs
;
TS
--------------------------------------------
2020-12-21 00:00:00.000 America/New_York EST
2021-03-18 00:00:00.000 America/New_York EDT
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.