Tino
Tino

Reputation: 65

Specify datetime2 format in Azure SQL data warehouse (synapse)

What is the correct way to specify the format of a datetime2 field when creating a table in Azure SQL data warehouse? I don't seem to be able to find an example in the documentation.

The data looks like this:

"2020-09-14T20:50:48.000Z"

CREATE TABLE [Foo].[Bar](
    ...
    MyDateTime datetime2(['YYYY-MM-DDThh:mm:ss[.fractional seconds]')
)

Upvotes: 0

Views: 3724

Answers (1)

Matt Usher
Matt Usher

Reputation: 1325

As Panagiotis notes, the underlying representation is an int/long for the actual date value. This is how RDBMS engines can quickly compute the delta between two dates (days between Monday and Friday is a simple subtraction problem). To answer your question, you simply would format your create table as:

CREATE TABLE [Foo].[Bar](
    ...
    MyDateTime datetime2
)

If you're interested in formatting the result in a query, you can look to the CONVERT or FORMAT functions. For example, if you wanted the format dd-mm-yyyy (Italian date), you could use either of the following:

SELECT
    CONVERT(VARCHAR, CURRENT_TIMESTAMP, 105)
    , FORMAT(CURRENT_TIMESTAMP, 'dd-MM-yyyy')

Note: CONVERT is generally faster than FORMAT and is the recommended approach if you have a date format that is supported. This is because the FORMAT function relies on the CLR which will include a context/process jump.

Upvotes: 0

Related Questions