BigQuery 将月年列转换为时间戳或日期
BigQuery convert Month Year column to timestamp or date
我有一个table
Year_month amount
08-2021 100
09-2021 200
10-2021 300
我想添加一列Month
成为
Year_month Month amount
08-2021 01-08-2021 100
09-2021 01-09-2021 200
10-2021 01-10-2021 300
您可以使用解析日期函数
SELECT PARSE_DATE("%m-%Y", "08-2021")
这取决于您拥有的数据类型。检查以下代码:
with sodata as (
select '08-2021' as Year_month_string, PARSE_DATE("%m-%Y", "08-2021") as Year_month_date, 100 amount,
union all
select '09-2021',PARSE_DATE("%m-%Y", "09-2021"), 200
union all
select '10-2021',PARSE_DATE("%m-%Y", "10-2021"), 300
)
SELECT Year_month_string,FORMAT('01-%s',Year_month_string) as using_format,FORMAT_DATE("%d-%m-%Y", Year_month_date) as using_format_date,amount from sodata
输出
Row
Year_month
using_format
using_format_date
amount
1
08-2021
01-08-2021
01-08-2021
100
2
09-2021
01-09-2021
01-09-2021
200
3
10-2021
01-10-2021
01-10-2021
300
要查看有关所用函数的更多详细信息,请转到此链接:
我有一个table
Year_month amount
08-2021 100
09-2021 200
10-2021 300
我想添加一列Month
成为
Year_month Month amount
08-2021 01-08-2021 100
09-2021 01-09-2021 200
10-2021 01-10-2021 300
您可以使用解析日期函数
SELECT PARSE_DATE("%m-%Y", "08-2021")
这取决于您拥有的数据类型。检查以下代码:
with sodata as (
select '08-2021' as Year_month_string, PARSE_DATE("%m-%Y", "08-2021") as Year_month_date, 100 amount,
union all
select '09-2021',PARSE_DATE("%m-%Y", "09-2021"), 200
union all
select '10-2021',PARSE_DATE("%m-%Y", "10-2021"), 300
)
SELECT Year_month_string,FORMAT('01-%s',Year_month_string) as using_format,FORMAT_DATE("%d-%m-%Y", Year_month_date) as using_format_date,amount from sodata
输出
Row | Year_month | using_format | using_format_date | amount |
---|---|---|---|---|
1 | 08-2021 | 01-08-2021 | 01-08-2021 | 100 |
2 | 09-2021 | 01-09-2021 | 01-09-2021 | 200 |
3 | 10-2021 | 01-10-2021 | 01-10-2021 | 300 |
要查看有关所用函数的更多详细信息,请转到此链接: