从日期开始CONCAT年和月,并添加自定义日期

时间:2018-11-27 05:26:06

标签: sql date google-bigquery concat

我正在尝试合并日期的年和月,并在Google bigQuery中添加自定义日期。有可能吗?

我的查询

SELECT  
CONCAT(((EXTRACT(YEAR FROM reportdt)), '-', (EXTRACT(MONTH FROM reportdt)), '01')
) AS 
yo,
brandnm,
EXTRACT(YEAR FROM reportdt) as the_year,
EXTRACT(MONTH FROM reportdt) as the_month 
FROM  table
WHERE reportdt BETWEEN '2018-05-01' AND '2018-11-30' 

我的错误:

"No matching signature for function CONCAT for argument types: STRUCT<INT64, STRING, INT64>. Supported signatures: CONCAT(STRING, [STRING, ...]); CONCAT(BYTES, [BYTES, ...]) at [2:1]"

的预期结果

SELECT
CONCAT(((EXTRACT(YEAR FROM reportdt)), '-', (EXTRACT(MONTH FROM reportdt)), '01') AS yo

2018-05-01

请帮助!

2 个答案:

答案 0 :(得分:3)

改用DATE_TRUNC(reportdt, MONTH)

因此您的查询将如下所示

#standardSQL
SELECT  
  DATE_TRUNC(reportdt, MONTH) AS yo,
  brandnm,
  EXTRACT(YEAR FROM reportdt) AS the_year,
  EXTRACT(MONTH FROM reportdt) AS the_month 
FROM  `project.dataset.table`
WHERE reportdt BETWEEN '2018-05-01' AND '2018-11-30'    

如果由于某种原因您必须在此处使用CONCAT-参见下文

#standardSQL
SELECT  
  CONCAT(CAST((EXTRACT(YEAR FROM reportdt)) AS STRING), '-', CAST((EXTRACT(MONTH FROM reportdt)) AS STRING), '-01') AS yo,
  brandnm,
  EXTRACT(YEAR FROM reportdt) AS the_year,
  EXTRACT(MONTH FROM reportdt) AS the_month 
FROM  `project.dataset.table`
WHERE reportdt BETWEEN '2018-05-01' AND '2018-11-30' 

答案 1 :(得分:0)

您可以使用CAST()

在下面尝试
SELECT  
CONCAT((CAST((EXTRACT(YEAR FROM reportdt)) as STRING), '-', CAST((EXTRACT(MONTH FROM reportdt)) as STRING), '01')
) AS 
yo,
brandnm,
EXTRACT(YEAR FROM reportdt) as the_year,
EXTRACT(MONTH FROM reportdt) as the_month 
FROM  table
WHERE reportdt BETWEEN '2018-05-01' AND '2018-11-30'