将日期时间戳转换为Presto

发布于 2025-02-12 15:33:53 字数 628 浏览 0 评论 0原文

有什么方法可以从2022-06-15 10:21:05.698000000转换为2022-06-15 10:21:05格式?

我在Hive中有数据(数据类型为字符串),其中包含此类数据2022-06-15 10:21:05.698000000。我需要将这些数据插入Oracle,在Oracle数据类型中是日期。在选择Hive的数据时,我正在使用以下查询。

select hive_date,cast(coalesce(substr(A.hive_date, 1,19),substr(A.hive_date2,1,19)) as timestamp) 
as oracle_date from test A limit 10;

它显示以下输出。

hive_date                                                  oracle_date
2022-06-15 10:21:05.698000000          |                   2022-06-15 10:21:05.000

我想将其转换为 2022-06-15 10:21:05 ,这样我就可以将其插入Oracle中。有人可以建议我吗?

is there any way to convert from 2022-06-15 10:21:05.698000000 to this 2022-06-15 10:21:05 format?

I have data in hive (Datatype is string) which contains data like this 2022-06-15 10:21:05.698000000. I need to insert this data in oracle, in oracle data type is date. I am using below query while selecting the data from hive.

select hive_date,cast(coalesce(substr(A.hive_date, 1,19),substr(A.hive_date2,1,19)) as timestamp) 
as oracle_date from test A limit 10;

It's showing below output.

hive_date                                                  oracle_date
2022-06-15 10:21:05.698000000          |                   2022-06-15 10:21:05.000

I want to convert this till seconds 2022-06-15 10:21:05 so i can insert into it in oracle. Can someone plz suggest me.

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

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

发布评论

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

评论(2

箜明 2025-02-19 15:33:54

您可以使用 date function 和date_format

select date_format(date_parse('2022-06-15 10:21:05.698000000', '%Y-%m-%d %H:%i:%s.%f'), '%Y-%m-%d %H:%i:%s')

输出:

_COL0
2022-06-15 10:21:05

我也建议在操作之前使用cocce,即原始尝试cocece(substr(a.hive_date,1,19,1,19),substr(a.hive_date2,1,19) ) - > substr(cocce(a.hive_date,a.hive_date2),1,19)

也可能只需在数据上使用trim,例如:

substr(trim(coalesce(A.hive_date, A.hive_date2)), 1, 19)

或:

select date_format(
        date_parse(
            trim(coalesce(A.hive_date, A.hive_date2)),
            '%Y-%m-%d %H:%i:%s.%f'
        ),
        '%Y-%m-%d %H:%i:%s'
    )

You can use date functions - date_parse and date_format:

select date_format(date_parse('2022-06-15 10:21:05.698000000', '%Y-%m-%d %H:%i:%s.%f'), '%Y-%m-%d %H:%i:%s')

Output:

_col0
2022-06-15 10:21:05

Also I would suggest using coalesce before manipulations i.e. in original attempt coalesce(substr(A.hive_date, 1,19),substr(A.hive_date2,1,19)) -> substr(coalesce(A.hive_date, A.hive_date2), 1, 19)

Also possibly you need to just use trim on the data, like:

substr(trim(coalesce(A.hive_date, A.hive_date2)), 1, 19)

Or:

select date_format(
        date_parse(
            trim(coalesce(A.hive_date, A.hive_date2)),
            '%Y-%m-%d %H:%i:%s.%f'
        ),
        '%Y-%m-%d %H:%i:%s'
    )
梓梦 2025-02-19 15:33:54

您可能需要的只是:

SELECT cast('2022-06-15 10:21:05.698000000' AS timestamp(0));
_COL0
2022-06-15 10:21:06

但是,如果需要是字符串,则截断:

SELECT date_format(timestamp '2022-06-15 10:21:05.698000000', '%Y-%m-%d %H:%i:%s');
_COL0
2022-06-15 10:21:05

或此方法回合:

SELECT cast(cast('2022-06-15 10:21:05.698000000' AS timestamp(0)) AS varchar);
_COL0
2022222222222222222222222222222222222222222222222222222222222222222222222年20222222222222222222222222222222 -06-15 10:21:06

It's possible that all you need is:

SELECT cast('2022-06-15 10:21:05.698000000' AS timestamp(0));
_col0
2022-06-15 10:21:06

But, if it needs to be a string, this truncates:

SELECT date_format(timestamp '2022-06-15 10:21:05.698000000', '%Y-%m-%d %H:%i:%s');
_col0
2022-06-15 10:21:05

Or this method rounds:

SELECT cast(cast('2022-06-15 10:21:05.698000000' AS timestamp(0)) AS varchar);
_col0
2022-06-15 10:21:06
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文