简体   繁体   English

在 Azure SQL 数据仓库(突触)中指定 datetime2 格式

[英]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?在 Azure SQL 数据仓库中创建表时指定 datetime2 字段格式的正确方法是什么? 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" “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.正如 Panagiotis 所指出的,底层表示是实际日期值的 int/long。 This is how RDBMS engines can quickly compute the delta between two dates (days between Monday and Friday is a simple subtraction problem).这就是 RDBMS 引擎如何快速计算两个日期之间的增量(星期一和星期五之间的天数是一个简单的减法问题)。 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.如果您对格式化查询中的结果感兴趣,可以查看CONVERTFORMAT函数。 For example, if you wanted the format dd-mm-yyyy (Italian date), you could use either of the following:例如,如果您想要格式dd-mm-yyyy (意大利日期),则可以使用以下任一方法:

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.注意: CONVERT通常比FORMAT快,如果您有受支持的日期格式,则推荐使用这种方法。 This is because the FORMAT function relies on the CLR which will include a context/process jump.这是因为FORMAT函数依赖于包含上下文/进程跳转的 CLR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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