简体   繁体   中英

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]')
)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM