我有一个名为opened_dt的列,其时间戳为字符串格式。
+----------------------------+
| opened_dt |
+----------------------------+
| 01/01/2015 21:50:00.000000 |
+----------------------------+
运行DESCRIBE TABLE 'myTable'
告诉我列的类型是string
+-----------+-----------+---------+
| col_name | data_type | comment |
+-----------+-----------+---------+
| opened_dt | string | null |
+-----------+-----------+---------+
我想做什么:
"Show me all entries starting 01.01.2019 until now".
Which I translate to "select * from 'table' where opened_dt >= 01.01.2019".
接下来我将opened_dt从日期转换为日期。
在使用:
SELECT cast(opened_dt AS timestamp)
给了我
+------------+
| opened_dt |
+------------+
| null |
+------------+
在使用:
SELECT to_date(opened_dt AS timestamp)
给了我
+---------------------------------+
| to_date('myTable'.`opened_dt`) |
+---------------------------------+
| null |
+---------------------------------+
我尝试的其他尝试,但给出错误的输出:
SELECT * FROM 'myTable' WHERE opened_dt >= '01/01/2019 00:00:00.000000'
SELECT * FROM 'myTable' WHERE opened_dt IN ('%2019%', '%2020%', '%2021%')
如何将字符串转换为日期以过滤所有小于01.01.2019的日期?
我正在寻找的东西在SQL DATABRICKS(其他答案是在spark)。
您可以使用to_timestamp
并提供与您的列的日期格式匹配的日期格式字符串转换为时间戳类型。
select *
from myTable
where to_timestamp(opened_dt, 'dd/MM/yyyy HH:mm:ss.SSSSSS') between to_timestamp('2019-01-01') and current_timestamp()