Reputation: 330
I have date saved in the below format :
+-------------------------+
| timex (varchar) |
+-------------------------+
| 20200303 |
+-------------------------+
I want the output/convert the value into date /time format in Databricks SQL.
Should be :
+-------------------------+
| timex |
+-------------------------+
| 2020-03-03 00:00:00.000 |
+-------------------------+
Tried :
select to_timestamp(timex)
--> results in 1970-xx-xxselect to_timestamp(timex, 'yyyy/MM/dd HH:mm:ss.ssssss')
--> says datatype in argument 1 should be in string or dateselect to_timestamp(CAST(timex AS STRING), 'yyyy/MM/dd HH:mm:ss.ssssss')
--> NULLselect to_date(timex)
--> NULLEquivalent in SQL - convert(datetime,cast(timex as nvarchar),103)
If I use cast(timex as string) as xx --> it works But, i I use to_timestamp(cast(timex as string), 'yyyyMMdd') as t --> it gives null.
I tried to format the string also to_timestamp(format_string('%8d',cast(timex as string)), 'yyyyMMdd') --> getting exception : IllegalFormatConversionException
Upvotes: 0
Views: 2350
Reputation: 42352
You need to convert to timestamp using to_timestamp
and then to the desired format using date_format
:
select date_format(to_timestamp(format_string('%8d', timex), 'yyyyMMdd'), 'yyyy-MM-dd HH:mm:ss.SSS')
from mytable;
Upvotes: 1