[英]Need to get in DATE Format (YYYY-MM-DD) from a NUMBER Data type column (123456789)
I have a column with datatype NUMBER(32,0)
.我有一个数据类型为
NUMBER(32,0)
的列。 This column actually stores the date and time stamp from the application.该列实际上存储来自应用程序的日期和时间戳。
I need to export this number
data type column as date
.我需要将此
number
数据类型列导出为date
。
ie, IE,
select column_name from table_name;
o/p:
1368046452000
I tried this option to export as Date,我试过这个选项导出为日期,
SELECT TO_DATE(
TO_TIMESTAMP('1970-01-01'
,'YYYY-MM-DD'
) + NUMTODSINTERVAL(column_name/1000, 'SECOND')
,'YYYY-MM-DD')
FROM table_name;
It gives me the output "15-JUL-23"
as expected.它按预期给了我输出
"15-JUL-23"
。
Question:题:
This table has around 900+ columns and in which 50+ columns are like this number data_type which stored the date and time in it.该表有大约 900 多列,其中 50 多列类似于这个数字 data_type,其中存储了日期和时间。 I need to convert/format these 50+ columns into
YYYY-MM-DD
.我需要将这 50 多列转换/格式化为
YYYY-MM-DD
。
How can I get/export/select all the column values from the table in which the above number data_type columns should return with YYYY-MM-DD
format?如何从表中获取/导出/选择所有列值,其中上述数字 data_type 列应以
YYYY-MM-DD
格式返回?
Please help!!请帮忙!!
Thanks, Karthik谢谢,卡尔提克
Your time appears to the a Unix timestamp which will measure milliseconds since 1970-01-01 in the UTC time zone.您的时间显示为 Unix 时间戳,该时间戳将测量自 1970-01-01 以来在UTC时区中的毫秒数。
You can use:您可以使用:
SELECT TIMESTAMP '1970-01-01 00:00:00 UTC' + column_name * INTERVAL '0.001' SECOND
FROM table_name;
Which will get the value as a TIMESTAMP
data type (since it can have fractional seconds).这将获得
TIMESTAMP
数据类型的值(因为它可以有小数秒)。
If you want it as a DATE
then use CAST
to convert it:如果你想要它作为
DATE
然后使用CAST
来转换它:
SELECT CAST(
TIMESTAMP '1970-01-01 00:00:00 UTC' + column_name * INTERVAL '0.001' SECOND
AS DATE
)
FROM table_name;
If you want it in a specific time zone (and not UTC) then use AT TIME ZONE 'your_tz'
:如果您想要在特定时区(而不是 UTC)使用它,请使用
AT TIME ZONE 'your_tz'
:
SELECT CAST(
( TIMESTAMP '1970-01-01 00:00:00 UTC' + column_name * INTERVAL '0.001' SECOND )
AT TIME ZONE 'PST'
AS DATE
)
FROM table_name;
So for your test data:所以对于你的测试数据:
CREATE TABLE table_name ( column_name ) AS
SELECT 1368046452000 FROM DUAL;
the query:查询:
SELECT ( TIMESTAMP '1970-01-01 00:00:00 UTC' + column_name * INTERVAL '0.001' SECOND )
AS UTC_TIMESTAMP,
CAST(
( TIMESTAMP '1970-01-01 00:00:00 UTC' + column_name * INTERVAL '0.001' SECOND )
AS DATE
) AS UTC_DATE,
CAST(
( TIMESTAMP '1970-01-01 00:00:00 UTC' + column_name * INTERVAL '0.001' SECOND )
AT TIME ZONE 'PST'
AS DATE
) AS PST_DATE
FROM table_name;
would output:会输出:
UTC_TIMESTAMP |UTC_时间戳 | UTC_DATE |
UTC_日期 | PST_DATE:-------------------------------- |:------------------ |:------------------ 2013-05-08 20:54:12.000000000 UTC |
PST_DATE:-------------------------------- |:---------------- ---- |:---------------- 2013-05-08 20:54:12.000000000 UTC | 2013-05-08 20:54:12 |
2013-05-08 20:54:12 | 2013-05-08 13:54:12
2013-05-08 13:54:12
Just for heck of less coding, create a function只是为了减少编码,创建一个函数
Create or replace function MyConvert(p_val in number ) return varchar2
as
begin
return TO_CHAR(TO_DATE('1970/01/01 00:00:00', 'YYYY/MM/DD HH24:MI:SS') +
NUMTODSINTERVAL(p_val / 1000,'SECOND'), 'YYYY-MM-DD');
end;
And then in your code just do然后在你的代码中做
SELECT MyConvert(col1) col1, ..... MyConvert(col50) col50 FROM table
The "date format" you are getting is not actually a date - this is a string representation of the date您得到的“日期格式”实际上不是日期 - 这是日期的字符串表示形式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.