Impala-本月的第一天

发布于 2025-01-26 14:50:10 字数 187 浏览 2 评论 0原文

我有时间戳格式的可变名称日期。我试图获得一个月的第一天。例如,如果日期为2022-02-27,则我需要的输出是2022-02-01。另一个示例是,如果日期是2022-01-15,那么我需要的输出是2022-01-01,

我需要该月份从该日期字段开始的第一天。我在Impala中这样做,我尝试了几件事,但它没有起作用。

感谢您的帮助!

I have variable name date in a timestamp format. I am trying to get the first day of the month. For example if the date is 2022-02-27 Then the output I need is 2022-02-01. Another example would be if the date is 2022-01-15 Then the output I need is 2022-01-01

I need the first day of that month from that date field. I am doing this in Impala, I tried couple of things, but it did not worked.

Thank you for your help!

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

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

发布评论

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

评论(1

长不大的小祸害 2025-02-02 14:50:10

您可以尝试以下任何一种 -

select 
trunc(datetime_col,'MM') first_day,
to_timestamp(concat(from_timestamp(datetime_col,'yyyyMM'),'01'),'yyyyMMdd') first_day_2,
to_timestamp(concat( substr(string_col,1,8),'01'),'yyyy-MM-dd') first_day_2_str -- when your date time column is a string

第一个方法 -
从时间戳列到一个月的第一天截断月份。

第二种方法 -
因此,您从时间戳列中获取Yyyymm,然后将01添加到该字符串中以获取第一天字符串。然后,您可以将其转换为时间戳。

第三种方法 -
类似于第二种方法,但substr用于切割月份。

you can try any one of below -

select 
trunc(datetime_col,'MM') first_day,
to_timestamp(concat(from_timestamp(datetime_col,'yyyyMM'),'01'),'yyyyMMdd') first_day_2,
to_timestamp(concat( substr(string_col,1,8),'01'),'yyyy-MM-dd') first_day_2_str -- when your date time column is a string

first method -
truncating Month part from timestamp column to first day of month.

second method -
so, you are getting yyyyMM from your timestamp column, and then add 01 to that string to get first day string. Then you can convert it to timestamp.

third method -
similar to second method but substr is used to cut the month part.

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