T-SQL 转换与转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/707335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:35:21  来源:igfitidea点击:

T-SQL Cast versus Convert

sqlsql-serverdatabasetsql

提问by BuddyJoe

What is the general guidance on when you should use CASTversus CONVERT? Is there any performance issues related to choosing one versus the other? Is one closer to ANSI-SQL?

关于何时应该使用CASTvs的一般指导是什么CONVERT?是否存在与选择一个或另一个相关的任何性能问题?一个更接近 ANSI-SQL 吗?

回答by Matthew Farwell

CONVERTis SQL Server specific, CASTis ANSI.

CONVERT是 SQL Server 特定的,CAST是 ANSI。

CONVERTis more flexible in that you can format dates etc. Other than that, they are pretty much the same. If you don't care about the extended features, use CAST.

CONVERT更灵活,因为您可以格式化日期等。除此之外,它们几乎相同。如果您不关心扩展功能,请使用CAST.

EDIT:

编辑:

As noted by @beruic and @C-F in the comments below, there is possible loss of precision when an implicit conversion is used (that is one where you use neither CAST nor CONVERT). For further information, see CAST and CONVERTand in particular this graphic: SQL Server Data Type Conversion Chart. With this extra information, the original advice still remains the same. Use CAST where possible.

正如@beruic 和@CF 在下面的评论中所指出的,使用隐式转换时可能会损失精度(即既不使用 CAST 也不使用 CONVERT 的转换)。有关更多信息,请参阅CAST 和 CONVERT,特别是此图:SQL Server 数据类型转换图表。有了这些额外信息,原始建议仍然保持不变。尽可能使用 CAST。

回答by jason saldo

Convert has a style parameter for date to string conversions.

Convert 有一个用于日期到字符串转换的样式参数。

http://msdn.microsoft.com/en-us/library/ms187928.aspx

http://msdn.microsoft.com/en-us/library/ms187928.aspx

回答by Abdeloihab Bourassi

CAST is standard SQL, but CONVERT is only for the dialect T-SQL. We have a small advantage for convert in the case of datetime.

CAST 是标准 SQL,但 CONVERT 仅适用于方言 T-SQL。在日期时间的情况下,我们有一个小的转换优势。

With CAST, you indicate the expression and the target type; with CONVERT, there's a third argument representing the style for the conversion, which is supported for some conversions, like between character strings and date and time values. For example, CONVERT(DATE, '1/2/2012', 101) converts the literal character string to DATE using style 101 representing the United States standard.

使用 CAST,您可以指明表达式和目标类型;对于 CONVERT,有第三个参数表示转换的样式,它支持某些转换,例如字符串与日期和时间值之间的转换。例如,CONVERT(DATE, '1/2/2012', 101) 使用代表美国标准的样式 101 将文字字符串转换为 DATE。

回答by Elaskanator

To expand on the above answercopied by Shakti, I have actually been able to measure a performance difference between the two functions.

为了扩展Shakti 复制的上述答案,我实际上已经能够衡量两个函数之间的性能差异。

I was testing performance of variations of the solution to this questionand found that the standard deviation and maximum runtimes were larger when using CAST.

我正在测试这个问题的解决方案变体的性能,发现使用CAST.

Runtimes in milliseconds*Times in milliseconds, rounded to nearest 1/300th of a second as per the precision of the DateTimetype

以毫秒为单位的运行时间*以毫秒为单位的时间,根据类型精度四舍五入到最接近的 1/300 秒DateTime

回答by Atario

Something no one seems to have noted yet is readability. Having…

似乎没有人注意到的一件事是可读性。拥有…

CONVERT(SomeType,
    SomeReallyLongExpression
    + ThatMayEvenSpan
    + MultipleLines
    )

…may be easier to understand than…

……可能比……更容易理解

CAST(SomeReallyLongExpression
    + ThatMayEvenSpan
    + MultipleLines
    AS SomeType
    )

回答by RakeshP

CAST uses ANSI standard. In case of portability, this will work on other platforms. CONVERT is specific to sql server. But is very strong function. You can specify different styles for dates

CAST 使用 ANSI 标准。在可移植性的情况下,这将适用于其他平台。CONVERT 特定于 sql server。但是是很强大的功能。您可以为日期指定不同的样式