Formatting a Date in BigQuery

纵饮孤独 提交于 2019-12-02 11:46:29

Below is for BigQuery Standard SQL and assumes your date field is of STRING type

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '20170726' date
)
SELECT 
  FORMAT_DATE('%m/%d/%Y', PARSE_DATE('%Y%m%d', date)) AS `date_string_formatted_as_MM_DD_YYYY`,
  FORMAT_DATE('%Y-%m-%d', PARSE_DATE('%Y%m%d', date)) AS `date_string_formatted_as_YYYY_MM_DD`
FROM `project.dataset.table`

with result

Row date_string_formatted_as_MM_DD_YYYY date_string_formatted_as_YYYY_MM_DD  
1   07/26/2017                          2017-07-26

You can try this for standardSql

PARSE_DATE('%Y%m%d', date) as date,

You'll want to use this instead. SELECT CONVERT(VARCHAR, [your_date_field] , XXX) where XXX is an integer value that tells CONVERT() which format you want your date to be. I believe you could use 101 for your purposes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!