如何将数字数据类型的20220401的列值转换为雪花中的2022-04-01

发布于 2025-01-27 17:38:52 字数 60 浏览 3 评论 0 原文

我有一个数字数据类型的雪花列列存储像20220401这样的值,现在我需要将其转换为日期格式。有人可以帮忙吗

I have a column of number data type in snowflake that stores values like 20220401, now I need to convert it into date format. Could someone please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉梦枕江山 2025-02-03 17:38:52

您可以使用to_date,但可以在char Datatype上使用,因此首先需要将列转换为char/string。

with data_cte(col1) as
(select 20220401::number)
select to_date(col1::string,'yyyymmdd') from data_cte

You can use to_date, but it works on char datatype, so first need to convert your column to char/string.

with data_cte(col1) as
(select 20220401::number)
select to_date(col1::string,'yyyymmdd') from data_cte
标点 2025-02-03 17:38:52

看到/说同样的话还有更多的方法:

to_date 允许您定义

select column1 as raw_str, 
    to_date(column1, 'yyyymmdd') as as_date
from values
    ('19900101'),
    ('20220511');

给出:

raw_str as_date
19900101 1990-01-01
20220511 2022-05-11

如果您有有时不遵循格式模式的字符串,则您可能想使用

select column1 as raw_str, 
    try_to_date(column1, 'yyyymmdd') as as_date
from values
    ('19900101'),
    ('not a date'),
    ('20220511');
raw_str as_date
1990010119001011990-01-01-01-01-01-01-01-01-01- snowflake.com/en/sql-referency/functions/try_to_date.html
错误
“> try_to_date “ rel =” nofollow noreferrer -05-11,

但鉴于您说的“数字输入”

select column1 as raw_number, 
    to_date(column1::text, 'yyyymmdd') as as_date
from values
    (19900101),
    (20220511);

现在我们有数字,但是to_date想要文本,因此我们将其施放给文本,因此可以使用解析器。

给予:

RAW_NUMBER AS_DATE
19,900,101 1990-01-01
20,220,511 2022-05-11

,如果您使用的是使用to_timestamp,它接受相同格式的字符串,裸体数字本身,它将认为您的意思是seconds,并给您带来的secoch,并给您带来的小小的微型数字,例如:类似:

select column1 as raw_number, 
    to_timestamp(column1) as as_timestamp
from values
    (19900101),
    (20220511);
RAW_NUMBER AS_TIMESTAMP
19,900,101 1970-08-19 07:48:21.000
20,220,511 1970-08-23 00:48:31.000,

这都是可以说,通过指定您的格式,以便您可以处理yyyymmdd或yyyyddmm或每个数据的方式。

Yet more way's of seeing/saying the same thing:

to_date allows you to define format string

select column1 as raw_str, 
    to_date(column1, 'yyyymmdd') as as_date
from values
    ('19900101'),
    ('20220511');

gives:

RAW_STR AS_DATE
19900101 1990-01-01
20220511 2022-05-11

if you have string that sometimes are not following the format pattern you might want to use try_to_date as this will not generate an error:

select column1 as raw_str, 
    try_to_date(column1, 'yyyymmdd') as as_date
from values
    ('19900101'),
    ('not a date'),
    ('20220511');
RAW_STR AS_DATE
19900101 1990-01-01
not a date null
20220511 2022-05-11

But given you said "number input"

select column1 as raw_number, 
    to_date(column1::text, 'yyyymmdd') as as_date
from values
    (19900101),
    (20220511);

now we have numbers, but to_date wants text, so we cast it to text, so the parser can be used.

giving:

RAW_NUMBER AS_DATE
19,900,101 1990-01-01
20,220,511 2022-05-11

Now if you use TO_TIMESTAMP which accepts the same format string, the naked number by itself it will think you mean epoch seconds, and give you wacky tiny number, like:

select column1 as raw_number, 
    to_timestamp(column1) as as_timestamp
from values
    (19900101),
    (20220511);
RAW_NUMBER AS_TIMESTAMP
19,900,101 1970-08-19 07:48:21.000
20,220,511 1970-08-23 00:48:31.000

which is all to say, cast it to string via ::text or the likes, and specify your format, so that you can handle yyyymmdd or yyyyddmm or how every your data is.

叹沉浮 2025-02-03 17:38:52
select to_date('02/14/2014', 'MM/DD/YYYY');

create table number_to_date(numbers integer);
insert into number_to_date values (20220401);
select to_date(to_char(numbers),'YYYYMMDD') from number_to_date;

更多详细信息:

select to_date('02/14/2014', 'MM/DD/YYYY');

create table number_to_date(numbers integer);
insert into number_to_date values (20220401);
select to_date(to_char(numbers),'YYYYMMDD') from number_to_date;

More details: https://docs.snowflake.com/en/sql-reference/functions/to_date.html#examples

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文