Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

Format string to datetime using Spark SQL

I am trying to convert and reformat a date column stored as a string using spark sql from something that looks like this...

30/03/20 02:00

to something that is a datetime column and looks like this...

2020-03-30 02:00 ('YYYY-MM-dd HH:mm')

I am not using python but simply writing sql in DBeaver directly to a spark datalake. Any help is greatly appreciated.

question from:https://stackoverflow.com/questions/66055042/format-string-to-datetime-using-spark-sql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use to_timestamp to parse string date into timestamp column and date_format to format it to desired pattern:

select date_format(to_timestamp(sting_date, 'dd/MM/yy HH:mm'), 'yyyy-MM-dd HH:mm') as date 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...