无法从此格式中提取年份(yyyy / mm / dd)且无法将格式更改为mm-dd-yyyy。我尝试过以下但是失败了。
with table1 as(
select "2017/1/1" as doj union all
select "2017/2/3" as doj union all
select "2017/3/5" as doj union all
select "2017/4/7" as doj union all
select "2017/6/9" as doj
)
select format_date("%m-%d-%Y",doj)doj from table1
Error: No matching signature for function FORMAT_DATE for argument types: STRING, STRING. Supported signature: FORMAT_DATE(STRING, DATE)
答案 0 :(得分:2)
以下是BigQuery Standard SQL
#standardSQL
WITH `project.dataset.table1` AS(
SELECT "2017/1/1" AS doj UNION ALL
SELECT "2017/2/3" AS doj UNION ALL
SELECT "2017/3/5" AS doj UNION ALL
SELECT "2017/4/7" AS doj UNION ALL
SELECT "2017/6/9" AS doj
)
SELECT
PARSE_DATE("%Y/%m/%d", doj) doj_as_date,
EXTRACT(YEAR FROM PARSE_DATE("%Y/%m/%d", doj)) year
FROM `project.dataset.table1`
答案 1 :(得分:1)
在格式化或提取年份之前,您需要首先使用parse_date
进行解析。这有效:
with table1 as(
select "2017/1/1" as doj union all
select "2017/2/3" as doj union all
select "2017/3/5" as doj union all
select "2017/4/7" as doj union all
select "2017/6/9" as doj
)
select
format_date("%m-%d-%Y", parse_date("%Y/%m/%d", doj)) as doj,
extract(YEAR from parse_date("%Y/%m/%d", doj)) as year
from table1
虽然取决于你对结果做了什么,但我建议只是解析,并将日期返回给客户端以提取年份并在需要时重新格式化。我知道有些情况不可行,例如:如果你需要为其他客户编写一个表,真的需要将日期作为特定格式的字符串。
如果您需要格式化日期,但可以选择格式,我绝对会使用ISO-8601而不是月份优先或第一天:)