Showing posts with label CAST VS CONVERT IN SQL. Show all posts
Showing posts with label CAST VS CONVERT IN SQL. Show all posts

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 datetime 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)

    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