SQL CONVERT

This function converts an expression from one data type to another. If the SQL Server cannot CONVERT an expression into the desired data type, then it returns an Error. For this SQL convert demonstration, we use the Employee table.

Employee Table Data 0

SQL Server CONVERT Function Syntax

The syntax of the SQL CONVERT Function

CONVERT (Data_Type [(Length)], Expression, [Style])

-- For example
SELECT CONVERT (VARCHAR(50), 245) AS [resultName]
FROM [Source]
  • Data_Type: Specify the Data Type to which you want to transform an expression
  • Length: It is an optional parameter of integer type. You can use this parameter to specify the length of the target data type. By default, it is 30.
  • An expression that you want to convert into the desired data type.
  • Style: use this optional parameter of integer type to define the style.

SQL CONVERT Function to format Date

The list of formatting styles is available in the SQL Server CONVERT function.

Without Century (yy)With Century (yyyy)StandardInput/Output
0 to 100This is the Default for both datetime and smalldatetimemon dd yyyy hh:miAM (or PM)
1101The U.S.1 = mm/dd/yy
101 = mm/dd/yyyy
2102ANSI2 = yy.mm.dd
102 = yyyy.mm.dd
3103British/French3 = dd/mm/yy
103 = dd/mm/yyyy
4104German4 = dd.mm.yy
104 = dd.mm.yyyy
5105Italian5 = dd-mm-yy
105 = dd-mm-yyyy
61066 = dd mon yy
106 = dd mon yyyy
71077 = Mon dd, yy
107 = Mon dd, yyyy
8108hh:mi:ss
9 or 109Default + millisecondsmon dd yyyy hh:mi:ss:mmmmAM (or PM)
10110USA10 = mm-dd-yy
110 = mm-dd-yyyy
11111JAPAN11 = yy/mm/dd
111 = yyyy/mm/dd
12112ISO12 = yymmdd
112 = yyyymmdd
13 or 113Europe Default + milliseconddd mon yyyy hh:mi:ss:mmm(24h)
14114hh:mi:ss:mmm(24h)
20 or 120ODBC canonicalyyyy-mm-dd hh:mi:ss(24h)
21 or 121ODBC canonical with milliseconds. This is the Default for time, date, datetime2, and datetimeoffsetyyyy-mm-dd hh:mi:ss.mmm(24h)
126ISO8601yyyy-mm-ddThh:mi:ss.mmm (no Spaces)
127ISO8601 with time zone Zyyyy-mm-ddThh:mi:ss.mmmZ (no Spaces)
130Hijridd mon yyyy hh:mi:ss:mmmAM
131Hijridd/mm/yyyy hh:mi:ss:mmmAM

SQL CONVERT Function to Format DATE

In this example, we use the SQL CONVERT function on GETDATE() to return the date in different formats.

SELECT CONVERT(VARCHAR(50), GETDATE()) AS 'Result 1';

SELECT CONVERT(VARCHAR(50), GETDATE(), 100) AS 'Result 2';
SELECT CONVERT(VARCHAR(50), GETDATE(), 101) AS 'Result 3';
 
SELECT CONVERT(VARCHAR(50), GETDATE(), 102) AS 'Result 4';
SELECT CONVERT(VARCHAR(50), GETDATE(), 103) AS 'Result 5';
 
SELECT CONVERT(VARCHAR(50), GETDATE(), 104) AS 'Result 6';
SELECT CONVERT(VARCHAR(50), GETDATE(), 105) AS 'Result 7';
SELECT CONVERT(VARCHAR(50), GETDATE(), 110) AS 'Result 8';
SQL CONVERT Example to Format DATE 1

SQL Server CONVERT Money Format Example

If an expression is a money or small money, then we can use the style argument to stylize. The list of money formatting styles that are available in the SQL Server CONVERT function is.

ValueOutput
0It will only return two digits after the decimal point
1It will separate every three digits by placing a comma. It will also return two digits after the decimal point.
2This return four digits after the decimal point
126This is equivalent to 2 when you are changing to char or varchar.
DECLARE @Sale MONEY
SET @Sale = 1234567.98723

SELECT CONVERT(VARCHAR(50), @Sale) AS 'Result 1';

SELECT CONVERT(VARCHAR(50), @Sale, 0) AS 'Result 2';

SELECT CONVERT(VARCHAR(50), @Sale, 1) AS 'Result 3';
SELECT CONVERT(VARCHAR(50), @Sale, 2) AS 'Result 4';

SELECT CONVERT(VARCHAR(50), @Sale, 256) AS 'Result 5';
SQL CONVERT Money Format Example 2

SQL Convert float values Example

We can use the style argument when an expression is float or real. The following are the formatting styles that are available in the SQL CONVERT function.

ValueOutput
0This returns a maximum of 6 digits
1It always returns 16 digits. Always use Scientific notation
2It always returns 16 digits. Always use in Scientific notation
3It returns eight digits. Always use Scientific notation
DECLARE @Sale FLOAT
SET @Sale = 1234567.98723

SELECT CONVERT(VARCHAR(50), @Sale) AS 'Result 1';

SELECT CONVERT(VARCHAR(50), @Sale, 0) AS 'Result 2';

SELECT CONVERT(VARCHAR(50), @Sale, 1) AS 'Result 3';
SELECT CONVERT(VARCHAR(50), @Sale, 2) AS 'Result 4';

SELECT CONVERT(VARCHAR(50), @Sale, 3) AS 'Result 5';
SQL Server function to convert Float to Varchar Example 3

Null Values Example

In this SQL convert function example, we will work with NULL values.

DECLARE @str AS VARCHAR(50)
SET @str = NULL

SELECT CONVERT(INT, @str) AS Result;

SELECT CONVERT(INT, NULL) AS Result;
Null to Integer 4

Suppose we use the SQL Server Convert function to change the ‘Tutorial Gateway’ string to date time. As you can see, it is impossible, so it returns an Error as output.

SELECT CONVERT(INT, 'Tutorial Gateway') AS Result;

Execute the above Transact query.

Msg 245, Level 16, State 1, Line 2
Failed converting the Varchar value 'Tutorial Gateway' to data type int.

We will apply this to our Employee table in this SQL Server Convert Function example.

SELECT TOP 1000 [EmpID]
      ,[FirstName]
      ,[LastName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome] AS [Yearly Income]
      ,CONVERT(VARCHAR(50),[Sales], 2) AS [Sale Amount]
      ,CONVERT(VARCHAR(50), [HireDate], 113) AS [Hire Date]
  FROM [Employee]
SQL CONVERT EXAMPLE on a Employee table Columns 6
Categories SQL