cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
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

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group