Oct 1, 2013
CAST vs CONVERT in Sql Server
CAST() Function:
The Cast() function is used to convert a data type variable or data from one data type to another data type.
The Cast() function provides a data type to a dynamic parameter (?) or a NULL value.
Syntax:
CAST ( [Expression] AS Datatype)
CONVERT() Function:
When you convert 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 a date time type to a varchar type. The Convert function is used for such things. The CONVERT() function can be used to display date/time data in various formats.
Syntax:
CONVERT(data_type(length), expression, style)
The basic differences are
Cast:
Cast is ANSII Standard
Cast cannot be used for Formatting Purposes.
Cast cannot convert a datetime to specific format
Convert
Convert is Specific to SQL SERVER
Convert can be used for Formatting Purposes.For example Select convert (varchar, datetime, 101)
Convert can be used to convert a datetime to specific format
Examples:
CAST:
SELECT CAST ( 10.20 AS INT )
Output
10
Above example converts 10.20 float value into an integer value and returns 10.
CONVERT:
CONVERT(VARCHAR(19),GETDATE())
O/p:Nov 04 2011 11:45 PM
CONVERT(VARCHAR(10),GETDATE(),10)
O/p:11-04-11
Labels:
sqlserver faqs
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment