Skip to content

Instantly share code, notes, and snippets.

@rococtz
Last active October 6, 2023 02:19
Show Gist options
  • Save rococtz/6282c802004913081b7a2fd896d163db to your computer and use it in GitHub Desktop.
Save rococtz/6282c802004913081b7a2fd896d163db to your computer and use it in GitHub Desktop.

To transform your Databricks table with columns "28 September, 2023", "04", and "23" into a Databricks date format using SQL, you can use SQL queries within Databricks. You'll need to concatenate the columns to create a string representing the full timestamp and then cast it to a timestamp type. Here's the SQL query to achieve this:

SELECT 
  TO_TIMESTAMP(CONCAT_WS(' ', `28 September, 2023`, `04`, `23`), 'dd MMMM, yyyy HH:mm') AS timestamp_col
FROM
  yourTable;

In this SQL query:

  • CONCAT_WS(' ', 28 September, 2023, 04, 23) concatenates the three columns with spaces to create a string in the format '28 September, 2023 04 23'.
  • TO_TIMESTAMP(...) converts this concatenated string into a timestamp using the specified format 'dd MMMM, yyyy HH:mm'.
  • The result is selected as timestamp_col, which will hold the timestamp in Databricks date format.

Replace yourTable with the actual name of your table. This SQL query will produce a result set with a column named timestamp_col containing the transformed timestamps in Databricks date format.


To transform the columns "28 September, 2023," "04," and "23" into a Databricks date format in Excel, you can follow these steps:

Assuming your date is in cell A1, the hour is in cell B1, and the minute is in cell C1, you can use a formula to concatenate them and then parse the result into a date format.

  1. In an empty cell, enter the following formula:
=DATE(RIGHT(A1, 4), MATCH(MID(A1, 4, FIND(" ", A1) - 4), {"January","February","March","April","May","June","July","August","September","October","November","December"}, 0), LEFT(A1, 2))

This formula extracts the year, month, and day from cell A1, which contains the date in the "28 September, 2023" format.

  1. In another empty cell, enter the following formula:
=TIMEVALUE(TEXT(B1,"00")&":"&TEXT(C1,"00"))

This formula concatenates the hour (cell B1) and minute (cell C1) and converts the result into a time value.

  1. In a third empty cell, enter the following formula to combine the date and time:
=A2 + A3

In this formula, A2 contains the date from step 1, and A3 contains the time from step 2.

The third cell will now contain the combined date and time in Excel date/time format.

  1. Format the cell with the combined date and time as you wish (e.g., as "dd mmmm, yyyy HH:mm") by right-clicking the cell, selecting "Format Cells," and choosing the desired date and time format.

Now, you have the date and time in the desired Databricks date format in Excel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment