What is the general guidance on when you should use CAST versus CONVERT ? Is there any performance issues related to choosing one versus the other? Is one closer to ANSI-SQL?
CONVERT is 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 .
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 CONVERT and 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.
To expand on the above answercopied by Shakti, I have actually been able to measure a performance difference between the two functions.
I was testing performance of variations of the solution to this question and found that the standard deviation and maximum runtimes were larger when using CAST .
*Times in milliseconds, rounded to nearest 1/300th of a second as per the precision of the DateTime type
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.
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 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
You should also not use CAST for getting the text of a hash algorithm. CAST(HASHBYTES('...') AS VARCHAR(32)) is not the same as CONVERT(VARCHAR(32), HASHBYTES('...'), 2) . Without the last parameter, the result would be the same, but not a readable text. As far as I know, You cannot specify that last parameter in CAST .
7条答案
按热度按时间vi4fp9gy1#
CONVERT
is SQL Server specific,CAST
is ANSI.CONVERT
is 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, useCAST
.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 CONVERT and 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.
p5fdfcr12#
CONVERT
has a style parameter for date to string conversions.https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
5m1hhzi43#
To expand on the above answercopied by Shakti, I have actually been able to measure a performance difference between the two functions.
I was testing performance of variations of the solution to this question and found that the standard deviation and maximum runtimes were larger when using
CAST
.*Times in milliseconds, rounded to nearest 1/300th of a second as per the precision of the
DateTime
typemi7gmzs64#
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.
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.
az31mfrm5#
Something no one seems to have noted yet is readability. Having…
…may be easier to understand than…
yks3o0rb6#
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
ncecgwcz7#
You should also not use
CAST
for getting the text of a hash algorithm.CAST(HASHBYTES('...') AS VARCHAR(32))
is not the same asCONVERT(VARCHAR(32), HASHBYTES('...'), 2)
. Without the last parameter, the result would be the same, but not a readable text. As far as I know, You cannot specify that last parameter inCAST
.