BigQuery converting string to datetime

北城以北 提交于 2021-01-28 21:20:47

问题


I'm using BigQuery to try I have a table with a string column called 'DATAUTILIZACAO' that has the following sample values:

02/11/16 12:19:08,000000

02/11/16 17:39:41,000000

The text is formatted as "DD/MM/YY HH:mm:ss" I need to create a new column of type DATETIME containing the value of DATAUTILIZACAO.

How can I get the value from DATAUTILIZACAO format it as "YYYY-MM-DD HH:MI:SS" and save it to the new column?

Can I do that using Query+UDF directly ?

Thanks,

Leo


回答1:


Try below (for Standard SQL - see Enabling Standard SQL and Migrating from legacy SQL)

WITH TheTable AS (
  SELECT '02/11/16 12:19:08,000000' AS DATAUTILIZACAO UNION ALL
  SELECT '02/11/16 17:39:41,000000' AS DATAUTILIZACAO 
)
SELECT DATAUTILIZACAO, PARSE_TIMESTAMP('%d/%m/%y %H:%M:%S,000000', DATAUTILIZACAO) AS parsedDATAUTILIZACAO
FROM TheTable

see more on PARSE_TIMESTAMP()




回答2:


You can use PARSE_DATETIME to get a DATETIME value from the strings using standard SQL (uncheck "Use Legacy SQL" under "Show Options"). For example,

WITH T AS (
  SELECT DATAUTILIZACAO
  FROM UNNEST(['02/11/16 12:19:08,000000',
               '02/11/16 17:39:41,000000']) AS DATAUTILIZACAO
)
SELECT PARSE_DATETIME('%D %T,000000', DATAUTILIZACAO) AS dt
FROM T;


来源:https://stackoverflow.com/questions/40411938/bigquery-converting-string-to-datetime

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