简体   繁体   English

需要从数字数据类型列 (123456789) 获取日期格式 (YYYY-MM-DD)

[英]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

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

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.

相关问题 从日期列中按日期分组,格式为 yyyy-mm-dd - group by date from date column in yyyy-mm-dd format 将 mm/dd/yyyy 传递给 yyyy-mm-dd 日期格式 - Pass mm/dd/yyyy to yyyy-mm-dd date format SQLLDR Oracle数据导入,日期格式= yyyy-mm-dd - SQLLDR Oracle Data Import, Date Format = yyyy-mm-dd 如何在 PL/SQL 中将日期格式从 MM/DD/YYYY 更改为 YYYY-MM-DD? - How to change the date format from MM/DD/YYYY to YYYY-MM-DD in PL/SQL? 在SQL中,为什么插入语句中的DATE数据类型的格式为&#39;YYYY-MM-DD&#39;,但会出现错误 - In SQL,Why is the format 'YYYY-MM-DD' for DATE data type in an Insert statement giving error MySQL将日期从mm-dd-yyyy转换为YYYY-MM-DD格式 - mysql convert date from mm-dd-yyyy to YYYY-MM-DD format pyspark sql 将日期格式从 mm/dd/yy hh:mm 或 yyyy-mm-dd hh:mm:ss 转换为 yyyy-mm-dd hh:mm 格式 - pyspark sql convert date format from mm/dd/yy hh:mm or yyyy-mm-dd hh:mm:ss into yyyy-mm-dd hh:mm format netezza 将 YYYY-MM-DD 格式的字符日期与 yyyy-mm-dd 格式的参数日期进行比较 - netezza compare char date in YYYY-MM-DD format to parameter date in yyyy-mm-dd format 如何在 sql 中将日期格式从 YYYY-MM-DD 转换为 YYYY-MM - How can I convert date format from YYYY-MM-DD to YYYY-MM in sql 将 SQL DATE 列从“00YY-MM-DD”转换为“YYYY-MM-DD” - Converting SQL DATE column from '00YY-MM-DD' to 'YYYY-MM-DD'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM