Formatting a Date in BigQuery

一个人想着一个人 提交于 2019-12-20 07:10:10

问题


I am querying 365 days worth of Google Analytics data and the data is exported as:

20170726

What I want is it parsed in some form:

2017-07-26
07/26/2017
07/26/2017

I believe I should be using the FORMAT_DATETIME clause/method to be using accomplishing this, and am have it like this:

SELECT
    FORMAT_DATETIME(%m/%d/%Y, date)

date being the field in Google Analytics.


回答1:


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



回答2:


You can try this for standardSql

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




回答3:


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.



来源:https://stackoverflow.com/questions/51218316/formatting-a-date-in-bigquery

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