cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

String converstion to datetimestamp format

refint650
New Contributor II

Hello

i'm converting hana sql code in databricks. we have 4 columns all in string format, start date, start time, end date, endtime..

1) what expression i can use to convert values of startdate & start time from string format to datetimeformat with AM/PM .? so later i can break final value in to two columns.

expected value:

Startdate ,, Starttime

2022-10-09 01-09-11 am

2022-10-09 -08-20-16 am (3rd row).

image

1 ACCEPTED SOLUTION

Accepted Solutions

@sri vs​ if the hours are 0-23 then you'll have to use HH instead of hh:

https://docs.databricks.com/spark/latest/spark-sql/language-manual/sql-ref-datetime-pattern.html#pat...

spark.sql("select date_format(to_timestamp(concat('20221009','010911'),'yyyyMMddHHmmss'),'yyyy-MM-dd hh:mm:ss a') as my_date").display()

View solution in original post

4 REPLIES 4

Matt101122
Contributor

@sri vs​ maybe concatenate the two strings and use to_timestamp with the format specified?

https://docs.databricks.com/spark/latest/spark-sql/language-manual/functions/to_timestamp.html

https://docs.databricks.com/spark/latest/spark-sql/language-manual/functions/concat.html

creating timestamp:

spark.sql("select to_timestamp(concat('20221009','010911'),'yyyyMMddhhmmss') as my_date").display()

 formatted with AM /PM:

spark.sql("select date_format(to_timestamp(concat('20221009','010911'),'yyyyMMddhhmmss'),'yyyy-MM-dd hh:mm:ss a') as my_date").display()

refint650
New Contributor II

Hello Matt

concat & to_timstamp function partially worked, values with 24 timestamp format not converted. any other approach i can think .?

image 

@sri vs​ if the hours are 0-23 then you'll have to use HH instead of hh:

https://docs.databricks.com/spark/latest/spark-sql/language-manual/sql-ref-datetime-pattern.html#pat...

spark.sql("select date_format(to_timestamp(concat('20221009','010911'),'yyyyMMddHHmmss'),'yyyy-MM-dd hh:mm:ss a') as my_date").display()

last expression worked as charm : thank you

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.