1
votes

I have a column named opened_dt with a timestamp in string format.

+----------------------------+
|        opened_dt           |
+----------------------------+
| 01/01/2015 21:50:00.000000 |
+----------------------------+

Running

DESCRIBE TABLE 'myTable'

tells me that the column is of type string

+-----------+-----------+---------+
| col_name  | data_type | comment |
+-----------+-----------+---------+
| opened_dt | string    | null    |
+-----------+-----------+---------+

What I want to do:

"Show me all entries starting 01.01.2019 until now".
Which I translate to "select * from 'table' where opened_dt >= 01.01.2019".

Following this I convert opened_dt from to date.

When using:

SELECT cast(opened_dt AS timestamp) 

just gives me

+------------+
| opened_dt  |
+------------+
| null       |
+------------+

When using:

SELECT to_date(opened_dt AS timestamp) 

just gives me

+---------------------------------+
| to_date('myTable'.`opened_dt`)  |
+---------------------------------+
| null                            |
+---------------------------------+

Other attemps I tried but gives false output:

SELECT * FROM 'myTable' WHERE opened_dt >= '01/01/2019 00:00:00.000000' 

SELECT * FROM 'myTable' WHERE opened_dt IN ('%2019%', '%2020%', '%2021%') 

How can I convert the string to date to filter all dates younger than 01.01.2019?

I am looking for something in SQL in DATABRICKS (other answers are in spark).

1

1 Answers

1
votes

You can cast to timestamp type using to_timestamp and providing a date format string that matches your column's date format.

select *
from myTable
where to_timestamp(opened_dt, 'dd/MM/yyyy HH:mm:ss.SSSSSS') between to_timestamp('2019-01-01') and current_timestamp()