A guide to the SQL Server Convert Function

Purpose: Converts from one data type to another data type.

Syntax

CONVERT (_New Data Type, Expression, Style_)

  • New Data Type: New data type to be converted too. For example: nvarchar, integer, decimal, date
  • Expression: Data to be converted.
  • Style: Format. For example: The style 110 is USA Date format mm-dd-yyyy

Example: Convert a Decimal Number to An Integer

SELECT CONVERT(INT, 23.456) as IntegerNumber

convert a decimal number to integer number

Note: The result is truncated.

Example: Convert a String to a Date

SELECT CONVERT(DATE, '20161030') as Date

convert a string to a date type

Example: Convert a Decimal to a String

SELECT CONVERT(nvarchar, 20.123) as StringData

convert a decimal to a string

Example: Convert an Integer Number to a Decimal Number

SELECT CONVERT(DECIMAL (15,3), 13) as DecimalNumber

convert an integer to a decimal number

Example: Convert a String to Date Format in USA Date Style

SELECT CONVERT(DATE, '20171030' , 110) To_USA_DateFormat

convert a string to date format in usa date style

More Information: