question

arkiboys avatar image
0 Votes"
arkiboys asked SaurabhSharma-msft commented

dateformat

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SaurabhSharma-msft avatar image
0 Votes"
SaurabhSharma-msft answered SaurabhSharma-msft commented

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.


image.png (4.0 KiB)
image.png (15.3 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

so make_date in notebook is the equivalent of formatdatepart in tsql, right?

0 Votes 0 ·

yes @arkiboys.

Please let me know if you find above reply useful. If yes, please 'Accept the answer' for the above reply. This will help other community members facing similar query to refer to this solution. Thanks.

0 Votes 0 ·
im-9629 avatar image
0 Votes"
im-9629 answered im-9629 edited

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



image.png (25.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.