Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

SQL CONVERT

by suresh

The SQL CONVERT function is used to convert an expression from one data type to another. If the SQL Server CONVERT function is unable to convert an expression into the desired data type, then it returns an Error.

For this SQL Server convert function demonstration, we use the Employee table.

SQL CONVERT EXAMPLE 0

SQL CONVERT Function Syntax

The syntax of the SQL Server CONVERT Function

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

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

SQL CONVERT Function to format Date

The list of formatting styles that are 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)
1101U.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
6106–6 = dd mon yy
106 = dd mon yyyy
7107–7 = Mon dd, yy
107 = Mon dd, yyyy
8108–hh: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)
14114–hh: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 Example to Format DATE

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

-- SQL SERVER CONVERT Function Example - Date Format
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 1

SQL CONVERT Money Format Example

If an expression is 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.

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 converting to char, or varchar
-- SQL SERVER CONVERT Function Example - Date Format
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 EXAMPLE 2

SQL Convert float values Example

When an expression is float or real, we can use the style argument. The following are the formatting styles that are available in the SQL Server CONVERT function.

ValueOutput
0This return maximum of 6 digits
1It returns eight digits. Always use in Scientific notation
2It always returns 16 digits. Always use in Scientific notation
3It is supported by SQL Server 2016. It returns 17 digits and is useful to perform the conversion without loosing
-- SQL SERVER CONVERT Function Example - Date Format
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 CONVERT EXAMPLE 3

SQL CONVERT Null Values Example 1

In this example, we will work with NULL values.

-- SQL SERVER CONVERT Function Example
DECLARE @str AS VARCHAR(50)
SET @str = NULL

SELECT CONVERT(INT, @str) AS Result;

SELECT CONVERT(INT, NULL) AS Result;
SQL CONVERT EXAMPLE 4

If we use the Convert function to convert the ‘Tutorial Gateway’ string to date time. As you can see, it is not possible so, this function is returning Error as output.

-- SQL SERVER CONVERT Function Example
SELECT CONVERT(INT, 'Tutorial Gateway') AS Result;
SQL CONVERT EXAMPLE 5

Sql Server CONVERT Function Example 3

In this example, we will apply the CONVERT function on our Employee table.

USE [SQL Tutorial]
GO
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 6

Placed Under: SQL

  • Install SQL Server
  • Install SQL Management Studio
  • Uninstall Management Studio
  • Install AdventureWorks Database
  • SQL Management Studio Intro
  • Connect SQL with sqlcmd utility
  • SQL Attach Database
  • SQL Detach Database
  • SQL Restore Database
  • Restore Database using BAK
  • SQL Rename Database with Files
  • Get SQL Database Names
  • SQL Create Table
  • SQL Rename Table
  • SQL Alter Table
  • SQL Add Column
  • SQL Rename Column
  • Get SQL Table Names in a DB
  • Find SQL Table Dependencies
  • Rename SQL Table & Column
  • SQL Global & Local Temp Table
  • SQL Table Variable
  • SQL Derived Table
  • SQL DATALENGTH
  • SQL Data Types
  • DML, DDL, DCL & TCL Cmds
  • SQL Query Builder
  • SQL ALIAS
  • SQL SELECT Statement
  • SQL SELECT DISTINCT
  • SQL SELECT INTO Statement
  • SQL INSERT Statement
  • SQL INSERT INTO SELECT
  • SQL BULK INSERT or BCP
  • SQL UPDATE Statement
  • SQL UPDATE from SELECT
  • SQL DELETE Statement
  • SQL TRUNCATE Table
  • SQL CASE Statement
  • SQL MERGE Statement
  • SQL Subquery
  • SQL CTE
  • SQL PIVOT
  • SQL UNPIVOT
  • SQL Clauses Examples
  • SQL TOP Clause
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Referential Integrity
  • SQL Check Constraint
  • SQL Unique Constraint
  • SQL Default Constraint
  • SQL Clustered Index
  • SQL Non Clustered Index
  • SQL Filtered Indexes
  • SQL COALESCE Function
  • SQL IS NOT NULL
  • SQL IS NULL Function
  • SQL ISNULL
  • SQL JOINS
  • SQL CROSS JOIN
  • SQL FULL JOIN
  • SQL SELF JOIN
  • SQL Outer Joins
  • SQL Cross Join Vs Inner Join
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL AND & OR Operators
  • SQL Arithmetic Operators
  • SQL BETWEEN Operator
  • SQL Comparison Operators
  • SQL LIKE
  • SQL EXCEPT
  • SQL EXISTS Operator
  • SQL NOT EXISTS Operator
  • SQL INTERSECT
  • SQL IN Operator
  • SQL NOT IN Operator
  • SQL UNION
  • SQL UNION ALL
  • SQL IF ELSE
  • SQL ELSE IF
  • SQL WHILE LOOP
  • SQL Nested While Loop
  • SQL BREAK Statement
  • SQL CONTINUE Statement
  • SQL GOTO Statement
  • SQL IIF Function
  • SQL CHOOSE Function
  • SQL Change Data Capture
  • SQL Table Partitioning
  • SQL Table Partition using SSMS
  • SQL TRY CATCH
  • SQL VIEWS
  • SQL User Defined Functions
  • SQL Alter User Defined Functions
  • SQL Stored Procedure Intro
  • Useful System Stored Procedures
  • SQL SELECT Stored Procedure
  • SQL INSERT Stored Procedure
  • SQL UPDATE Stored Procedure
  • Stored Procedure Return Values
  • Stored Procedure Output Params
  • Stored Procedure Input Params
  • Insert SP result into Temp Table
  • SQL Triggers Introduction
  • SQL AFTER INSERT Triggers
  • SQL AFTER UPDATE Triggers
  • SQL AFTER DELETE Triggers
  • SQL INSTEAD OF INSERT
  • SQL INSTEAD OF UPDATE
  • SQL INSTEAD OF DELETE
  • SQL STATIC CURSOR
  • SQL DYNAMIC CURSOR
  • SQL FORWARD_ONLY Cursor
  • SQL FAST_FORWARD CURSOR
  • SQL KEYSET CURSOR
  • SQL TRANSACTIONS
  • SQL Nested Transactions
  • SQL ACID Properties
  • Create SQL Windows Login
  • Create SQL Server Login
  • SQL Server Login Error
  • Create SQL Server Roles
  • SQL Maintenance Plan
  • Backup SQL Database
  • SQL Ranking Functions Intro
  • SQL RANK Function
  • SQL PERCENT_RANK Function
  • SQL DENSE_RANK Function
  • SQL NTILE Function
  • SQL ROW_NUMBER
  • SQL Aggregate Functions
  • SQL Date Functions
  • SQL Mathematical Functions
  • SQL String Functions
  • SQL CAST Function
  • SQL TRY CAST
  • SQL CONVERT
  • SQL TRY CONVERT
  • SQL PARSE Function
  • SQL TRY_PARSE Function
  • SQL Calculate Running Total
  • SQL Find Nth Highest Salary
  • SQL Reverse String
  • SQL FOR XML PATH
  • SQL FOR XML AUTO
  • SQL FOR XML RAW
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy