28

Possible Duplicate:
T-SQL Cast versus Convert

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

Community
  • 1
  • 1
Bhaumik Patel
  • 15,176
  • 5
  • 30
  • 33
  • 1
    What do you mean with "*in SQ*L"? SQL is a language and the `convert` thing is not a standard SQL function (operator?) –  Oct 16 '12 at 16:56

3 Answers3

25

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

Marcin S.
  • 11,161
  • 6
  • 50
  • 63
9

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)
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
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.

Setily
  • 814
  • 1
  • 9
  • 21
Seph
  • 8,472
  • 10
  • 63
  • 94