Formatting a Date in BigQuery

2020-05-04 23:02发布

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.

3条回答
Summer. ? 凉城
2楼-- · 2020-05-04 23:12

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
查看更多
Deceive 欺骗
3楼-- · 2020-05-04 23:13

You can try this for standardSql

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

查看更多
▲ chillily
4楼-- · 2020-05-04 23:26

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.

查看更多
登录 后发表回答