- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - CONVERT() Function
The SQL CONVERT() function transforms an expression from one data type to another. If the conversion is successful, the function returns the converted value; otherwise, it produces an error.
With the exception of the style option, the CONVERT() function is equivalent to the CAST() function. While CAST() is a component of ANSI SQL methods, which are accessible in many other databases, CONVERT() is unique to SQL Server and is not.
Syntax
Following is the syntax of the SQL CONVERT() function −
CONVERT(data_type(length), expression, style)
Parameters
This function accepts only four parameter. The same is described below −
data_type − The valid expression that to be converted.
length − It is the length of the resulting datatype which is optional.
expression − It is the datatype that we want to convert the expression to.
style − It is the format that used for the result which is optional.
The style value can be any one of the following values, let's look one by one:
Date and time styles for convert −
STYLE | STANDARD | RESULT |
---|---|---|
100 | datetime and smalldatetime | mon dd yyyy hh:miAM (or PM) |
101 | US | mm/dd/yyyy |
102 | ANSI | yyy.mm.dd |
103 | British/French | dd/mm/yyyy |
104 | German | dd.mm.yyyy |
105 | -- | dd mon yyyy |
106 | -- | Mon dd, yyyy |
107 | -- | Mon dd, yyyy |
108 | -- | hh:mi:ss |
109 | Default + milliseconds | mon dd yyyy hh:mi:ss:mmmAM (or PM) |
110 | USA | mm-dd-yyyy |
111 | Japan | yyyy/mm/dd |
112 | ISO | yyyymmdd |
113 | Europe Default + milliseconds | dd mon yyyy hh:mi:ss:mmm (24h) |
114 | -- | hh:mi:ss:mmm (24h) |
120 | ODBC canonical | yyyy-mm-dd hh:mi:ss (24h) |
121 | ODBC canonical (with milliseconds) default for time, date, datetime2, and datetimeoffset | yyyy-mm-dd hh:mi:ss.mmm (24h) |
126 | ISO8601 | yyyy-mm-ddThh:mi:ss.mmm (no spaces) |
127 | ISO8601 | yyyy-MM-ddThh:mm:ss.fffZ (nospaces) |
Float and real styles for convert −
STYLE | RESULT |
---|---|
0 | 6 digits max. Use scientific notation, when appropriate. |
1 | 8 digits. Always use scientific notation. |
2 | 16 digits. Always use scientific notation. |
Money and smallmoneystyles for convert −
STYLE | RESULT |
---|---|
0 | No commas and 2 decimals. Example: 28000.54. |
1 | Commas and 2 decimals. Example: 24,222.63. |
2 | No commas and 4 decimals. Example: 12345.6789. |
126 | Equivalent to 2, when converting to char(n) or varchar(n). |
Example
Let us try to convert the decimal into integer by using the following query −
SELECT CONVERT(int, 123.34) AS Result;
Output
When we execute the above query, the output is obtained as follows −
+--------------------------+ | Result | +--------------------------+ | 123 | +--------------------------+
Example
Let us convert the expression into varchar by using the following query −
SELECT CONVERT(varchar, 12344.34) AS varchar;
Output
On executing the above query, the output is displayed as follows −
+--------------------------+ | varchar | +--------------------------+ | 12344.34 | +--------------------------+
Example
In the following example we are going to convert the expression into the datetime by using the following query −
SELECT CONVERT(datetime, '2023-02-23') AS datetime;
Output
The output for the above query is produced as given below −
+--------------------------+ | datetime | +--------------------------+ | 2023-02-23 00:00:00.000 | +--------------------------+
Example
Look at the following example, where we are going to convert the float into integer by using the following query −
DECLARE @FLOAT AS Float SET @FLOAT = 12344.567 SELECT CONVERT(int, @FLOAT) AS ConvertedResult;
Output
If we compile and run the above query, the result is produced as follows −
+------------------------------------+ | ConvertedResult| +------------------------------------+ | 12344 | +------------------------------------+
Example
Letâs look into the another scenario, where we are going to change the float value to varchar by using the following query −
DECLARE @FLOAT AS Float SET @FLOAT = 1235.5687 SELECT CONVERT(varchar, @FLOAT) AS ConvertedResult;
Output
On executing the above query, it will generate the following output as shown below −
+------------------------------------+ | ConvertedResult| +------------------------------------+ | 1235.57 | +------------------------------------+
Example
Letâs consider another example, where we are going to convert the money into varchar by running the following query −
DECLARE @MONEY AS MONEY=$1234.56 SELECT CONVERT(Varchar,@MONEY,0) AS Style_0, CONVERT(Varchar,@MONEY,1) AS Style_1;
Output
When the query gets executed, it will generate the output as shown below −
+-----------------------------------+-------------------------------------+ | Style_0 | Style_1 | +-----------------------------------+-------------------------------------+ | 1234.56 | 1,234.56 | +-----------------------------------+-------------------------------------+
Example
In the following example, we are going to use the CONVERT() function along with the GETDATE() function to retrieve the current date and time by using the following query −
SELECT CONVERT(VARCHAR(20),GETDATE())AS Result;
Output
When the query gets executed, it will generate the output as shown below −
+------------------------------------+ | Result| +------------------------------------+ | Feb 23 2023 11:19AM | +------------------------------------+
Example
Considering the another scenario, where we are going to use the GETDATE() along with a CONVERT() function and mentioning the style by using the following query −
SELECT CONVERT(VARCHAR(11),GETDATE(),106) AS Result;
Output
When the query gets executed, it will generate the output as shown below −
+------------------------------------+ | Result| +------------------------------------+ | 23 Feb 2023 | +------------------------------------+
To Continue Learning Please Login
Login with Google