T-SQL 中的大括号

发布于 2024-12-04 17:12:19 字数 326 浏览 0 评论 0原文

我遇到过以下 t-sql:

SELECT {d'9999-12-31'}

它返回 9999-12-31 00:00:00.000

这似乎是将字符串文字的类型转换为DATETIME。我找不到有关此语法的任何文档,我想知道是否有任何变化,例如,如果我有一个文字 1 但想用 BIGINT 表示它> 不使用 CONVERT()/CAST()

谁能提供有关此语法的更多信息?谢谢。

I've come across the following t-sql:

SELECT {d'9999-12-31'}

Which returns 9999-12-31 00:00:00.000.

This seems to be converting the type of the string literal to a DATETIME. I can't find any documentation on this syntax and I'm wondering if there are any variations, for example if I have a literal 1 but want to represent this in a BIGINT without using CONVERT()/CAST().

Can anyone provide any further information on this syntax? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕色琉璃 2024-12-11 17:12:19

这些是 ODBC 转义序列。请参阅日期、时间和时间戳转义序列 了解更多详情。

也有类似的语法

uniqueidentifiers SELECT {guid '00000000-0000-0000-0000-000000000000'}

,以及该链接详细介绍的过程调用和其他一些构造。

关于您问题的其余部分,我不知道有任何方法可以将整数文字视为 bigint 或列出影响文字分配数据类型的所有方法的任何特定资源SQL 服务器。以下是一些方法。

;WITH cte(thing) AS
(
SELECT CAST(1 AS SQL_VARIANT) UNION ALL
SELECT $1 UNION ALL
SELECT 1e0 UNION ALL
SELECT 1.0000 UNION ALL
SELECT 2147483648 UNION ALL 
SELECT {ts '2011-09-15 01:23:56.123'}  UNION ALL
SELECT {d '2011-09-15'} UNION ALL
SELECT { t '13:33:41' }  UNION ALL
SELECT {guid '00000000-0000-0000-0000-000000000000'} UNION ALL
SELECT 'Foo' UNION ALL
SELECT N'Foo'
)
SELECT thing, 
       sql_variant_property(thing,'basetype') AS basetype,
       sql_variant_property(thing,'precision') AS precision, 
       sql_variant_property(thing,'scale') AS scale, 
       sql_variant_property(thing,'maxlength') AS maxlength
FROM cte

退货

thing                          basetype            precision   scale  maxlength
------------------------------ ------------------- ----------- ------ ---------
1                              int                 10          0      4
1.00                           money               19          4      8
1                              float               53          0      8
1.0000                         numeric             5           4      5
2147483648                     numeric             10          0      5
2011-09-15 01:23:56.123        datetime            23          3      8
2011-09-15 00:00:00.000        datetime            23          3      8
2011-09-15 13:33:41.000        datetime            23          3      8
00000000-0000-0000-0000-000000 uniqueidentifier    0           0      16
Foo                            varchar             0           0      3
Foo                            nvarchar            0           0      6

These are ODBC escape sequences. See Date, Time, and Timestamp Escape Sequences for more details.

There is also similar syntax for uniqueidentifiers

SELECT {guid '00000000-0000-0000-0000-000000000000'},

as well as procedure calls and some other constructs detailed off that link.

With regard to the rest of your question I'm not aware of any way of having an integer literal treated as a bigint or of any particular resource that lists all the ways of influencing how literals are assigned datatypes by SQL Server. Some ways are below.

;WITH cte(thing) AS
(
SELECT CAST(1 AS SQL_VARIANT) UNION ALL
SELECT $1 UNION ALL
SELECT 1e0 UNION ALL
SELECT 1.0000 UNION ALL
SELECT 2147483648 UNION ALL 
SELECT {ts '2011-09-15 01:23:56.123'}  UNION ALL
SELECT {d '2011-09-15'} UNION ALL
SELECT { t '13:33:41' }  UNION ALL
SELECT {guid '00000000-0000-0000-0000-000000000000'} UNION ALL
SELECT 'Foo' UNION ALL
SELECT N'Foo'
)
SELECT thing, 
       sql_variant_property(thing,'basetype') AS basetype,
       sql_variant_property(thing,'precision') AS precision, 
       sql_variant_property(thing,'scale') AS scale, 
       sql_variant_property(thing,'maxlength') AS maxlength
FROM cte

Returns

thing                          basetype            precision   scale  maxlength
------------------------------ ------------------- ----------- ------ ---------
1                              int                 10          0      4
1.00                           money               19          4      8
1                              float               53          0      8
1.0000                         numeric             5           4      5
2147483648                     numeric             10          0      5
2011-09-15 01:23:56.123        datetime            23          3      8
2011-09-15 00:00:00.000        datetime            23          3      8
2011-09-15 13:33:41.000        datetime            23          3      8
00000000-0000-0000-0000-000000 uniqueidentifier    0           0      16
Foo                            varchar             0           0      3
Foo                            nvarchar            0           0      6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文