Are CAST and CONVERT the same in SQL? [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:47:02

问题:

Possible Duplicate:
T-SQL Cast versus Convert

What is the major difference between CAST and CONVERT in SQL cause both performs data type conversion?

回答1:

CAST and CONVERT have similar functionality. CONVERT is specific to SQL Server, and allows for a greater breadth of flexibility when converting between date and time values, fractional numbers, and monetary signifiers. CAST is the more ANSI-standard of the two functions. Check this blog for examples of using both of those: http://sqltutorials.blogspot.com/2007/06/sql-cast-and-convert.html



回答2:

The convert function can do more complex conversions, for example converting a datetime value into varchar using a specific format:

convert(varchar(16), dateTimeValue, 120) 


回答3:

Assuming you're talking about SQL Server.

From http://msdn.microsoft.com/en-us/library/ms187928.aspx and http://msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx

Explicitly converts an expression of one data type to another. CAST and CONVERT provide similar functionality.

So yes, they are functionally the same. They just have different syntax that allows for more complex conversions or (subjectively) improved readability.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!