dateformat

arkiboys 8,301 Reputation points
2022-04-07T09:17:53.31+00:00

hello,
What is the equivalent of this T_sql in sql server but in databricks sql?
Thank you

CAST(
      DATEFROMPARTS(
          LEFT(table1.id,2)+2000,
          SUBSTRING(CAST(table1.id AS NVARCHAR(9)),3,2),
          SUBSTRING(CAST(table1.id AS NVARCHAR(9)),5,2)
          )
      AS DATE) AS DATA_REF
Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
1,381 questions
0 comments No comments
{count} votes

Accepted answer
  1. Saurabh Sharma 17,366 Reputation points Microsoft Employee
    2022-04-07T22:24:51.753+00:00

    Hi @arkiboys ,

    Thanks for using Microsoft Q&A !!
    If I understand you correctly, you have dates stored like below in a Databricks table and you want to construct a Date value in a Notebook using Databricks SQL using this stored data.
    191088-image.png
    You need to use the below SQL code in your Notebook to get the desired results -

    SELECT make_date(LEFT(Date,2)+2000,substr(Date,3,2),substr(Date,5,2)) as Date FROM Date_Table

    Result:
    191075-image.png

    You can refer to Databricks documentation to get more details on the above used functions.

    Please let me know if you have any questions.

    Thanks
    Saurabh

    ----------

    Please do not forget to "Accept the answer" wherever the information provided helps you to help others in the community.


1 additional answer

Sort by: Most helpful
  1. _im 1 Reputation point
    2022-04-08T00:02:23.203+00:00

    Alternatively, turn the string into a date as below:

    %sql  
    -- here we add "20" to the start of the original date string and turn it into a date type  
    SELECT to_date("20" || "121004", 'yyyyMMdd') AS dateType  
    

    191133-image.png

    0 comments No comments