DIFFERENCE BETWEEN CAST AND CONVERT(Cast vs Convert) in SQL Server

  • Cast
  1. Cast is  ANSII Standard
  2. Cast cannot be used for Formatting Purposes.
  3. Cast cannot convert a datetime to specific format
  • Convert
  1. Convert is Specific to SQL SERVER
  2. Convert can be used for Formatting Purposes.For example Select convert (varchar, datetime, 101)
  3. Convert can be used to convert a datetime to specific format
................................................................................................................................


SQL Server programmers can choose between two functions in SQL Server 7 and 2000 for converting expressions from one type to another. In many cases there will be a need within a stored procedure or other routine to convert data from, say, a datetime type to a varchar type; CONVERT and CAST are used for such things.
Because SQL Server provides both functions, there may be some confusion about which is best to use and under what circumstances. 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, meaning that while it's more portable (i.e., a function that uses CAST can be used in other database applications more or less as-is), it's also less powerful. CAST is also required when converting between decimal and numeric values to preserve the number of decimal places in the original expression. For those reasons, it's best to use CAST first, unless there is some specific thing that only CONVERT can provide

    0 comments:

    Post a Comment