Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

database

Possible Duplicate:
T-SQL Cast versus Convert

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

like image 515
Bhaumik Patel Avatar asked Oct 16 '12 16:10

Bhaumik Patel


People also ask

Is CAST the same as convert SQL?

CAST and CONVERT are two SQL functions used by programmers to convert one data type to another. 2. The CAST function is ANSI standard and is compatible to use in other databases while the CONVERT function is a specific function of the SQL server.

What is CAST and convert in SQL Server?

The cast and convert functions provide similar functionality. They are used to convert a value from one data type to another. So let's take a look at a practical example. The example is developed in SQL Server 2012 using the SQL Server Management Studio.

Which is faster convert or CAST?

Summary. Casting and converting are ways in which we can change a value from one type to another; casting is faster but more prone to errors, while conversion is more computationally expensive but also more forgiving.

What does CAST in SQL mean?

The SQL CAST function converts the data type of an expression to the specified data type. For a list of the data types supported by InterSystems SQL, see Data Types. CAST is similar to CONVERT, with these differences: CONVERT is more flexible than CAST.


3 Answers

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

like image 169
Marcin S. Avatar answered Oct 09 '22 11:10

Marcin S.


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)
like image 21
Guffa Avatar answered Oct 09 '22 13:10

Guffa


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.

like image 28
Seph Avatar answered Oct 09 '22 13:10

Seph