简体   繁体   English

SQL中如何将年周转为年月日?

[英]How to Convert Year-Week to Year-Month-Day in SQL?

Is there an efficient way or function to convert the week of the year to a datetime object?是否有有效的方法或 function 将一年中的星期转换为日期时间 object? For example, if I had "2018-03", how would I be able to convert it to "2018-01-21"?例如,如果我有“2018-03”,我如何将其转换为“2018-01-21”? (The third week of the year would correspond to the third week of the year, January 21st). (一年中的第三周对应于一年中的第三周,即 1 月 21 日)。

Any help is appreciated.任何帮助表示赞赏。 Thank you in advance!先感谢您!

consider below as an option考虑以下作为一个选项

select year_week, 
  date_add(
    date(cast(split(year_week, '-')[offset(0)] as int64), 1, 1),
    interval cast(split(year_week, '-')[offset(1)] as int64) * 7 - 1 day
  ) date
from data      

if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM