简体   繁体   English

SQL-将月份字符串'08 -2019'转换为时间戳数据类型-01-08-2019

[英]SQL - convert month string '08-2019' to timestamp datatype - 01-08-2019

I have a column - local_month - containing a string value "2019-08". 我有一列-local_month-包含字符串值“ 2019-08”。 Any ideas how I can convert this to a timestamp datatype? 有什么想法可以将其转换为时间戳数据类型吗?

My current approach was to get substrings of the value to yield a value of 01-08-2019. 我当前的方法是获取值的子字符串以产生01-08-2019的值。 This still is giving me an error when I try to use it as a timestamp. 当我尝试将其用作时间戳时,这仍然给我一个错误。

FORMAT_TIMESTAMP("%d-%m-%Y",TIMESTAMP(DATE(cast(SUBSTR(local_month,1,4) as int64),cast(SUBSTR(local_month,6,2) as int64),cast('01' as int64)))) as month  

You can use PARSE_DATE and then cast the output to timestamp as PARSE_DATE will return a DATE : 您可以使用PARSE_DATE ,然后将输出转换为时间戳,因为PARSE_DATE将返回DATE

with parse_date AS (SELECT PARSE_DATE("%m-%Y", '08-2019') as date)

SELECT FORMAT_TIMESTAMP('%d-%m-%Y', CAST(date as TIMESTAMP)) from parse_date

Which will output the following: 将输出以下内容:

Row |   f0_ 
------------------
1   |   01-08-2019

More information about PARSE_DATE function can be found in the public docs . 有关PARSE_DATE函数的更多信息,请PARSE_DATE 公共文档

Just use one of the PARSE_() functions. 只需使用PARSE_()函数之一。 In your case, PARSE_TIMESTAMP() : 就您而言, PARSE_TIMESTAMP()

SELECT PARSE_DATE('%m-%Y', '08-2019'),
       PARSE_DATETIME('%m-%Y', '08-2019'),
       PARSE_TIMESTAMP('%m-%Y', '08-2019')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM