REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
CONVERT – SQL Server function
SQL CONVERT – In this article you will learn how to use CONVERT function in SQL Server (Transact-SQL) with syntax and examples.
Description OF THE SQL CONVERT FUNCTION
In SQL Server (Transact-SQL), the CONVERT function converts an expression from one data type to another data type.
Syntax OF THE SQL CONVERT FUNCTION
Syntax of the CONVERT function in SQL Server (Transact-SQL):
CONVERT( type [(length) ], [ , style ] )
Parameters or arguments:
- type – the data type into which you want to convert the expression. It can be one of the following: bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real, datetime, smalldatetime, char, varchar, text, nchar, nvarchar, ntext, binary, varbinary, or image.
- length is optional. The length of the resulting data type for char, varchar, nchar, nvarchar, binary, and varbinary.
- expression – value for conversion to another data type.
- style – optional. The format used to convert between data types, such as date format or string format. This can be one of the following values: Convert datetime to character.
Meaning (no century) | Meaning (forever) | Explanation |
---|---|---|
0 | 100 | mon dd yyyy hh:miAM/PM (default) |
1 | 101 | mm/dd/yyyy (US standard) |
2 | 102 | yy.mm.dd (ANSI standard) |
3 | 103 | dd/mm/yy (British/French standard) |
4 | 104 | dd.mm.yy (German standard) |
5 | 105 | dd-mm-yy (Italian standard) |
6 | 106 | dd mon yy |
7 | 107 | Mon dd, yy |
8 | 108 | hh:mi:ss |
9 | 109 | mon dd yyyy hh:mi:ss:mmmAM/PM |
10 | 110 | mm-dd-yy (USA standard) |
11 | 111 | yy/mm/dd (Japan standard) |
12 | 112 | yymmdd (ISO standard) |
13 | 113 | dd mon yyyy hh:mi:ss:mmm (Europe standard — 24-hour) |
14 | 114 | hh:mi:ss:mmm (24-hour) |
20 | 120 | yyyy-mm-dd hh:mi:ss (ODBC canonical — 24-hour) |
21 | 121 | yyyy-mm-dd hh:mi:ss:mmm (ODBC canonical — 24-hour) |
126 | yyyy-mm-ddThh:mi:ss:mmm (ISO8601 standard) | |
127 | yyyy-mm-ddThh:mi:ss:mmmZ (ISO8601 standard) | |
130 | dd mon yyyy hh:mi:ss:mmmAM/PM (Hijri standard) | |
131 | dd/mm/yy hh:mi:ss:mmmAM/PM (Hijri standard) |
Converting float into real
Meaning | Explanation |
---|---|
0 | Max. 6 digits (default) |
1 | 8 digits |
2 | 16 digits |
Converting money into character
Meaning | Explanation |
---|---|
0 | Separator not comma, 2 digits to the right of the decimal (i.e. 1234.56) |
1 | Comma separator, 2 digits to the right of the decimal (i.e.: 1,234.56) |
2 | Separator not comma, 4 digits to the right of the decimal (i.e. 1234.5678) |
Note:
- When converting from float and numeric to integer, the CONVERT function truncates the result. For other conversions CONVERT will round up the result.
- See also the CAST function.
Application
The CONVERT function can be used in the following versions of SQL Server (Transact-SQL):
SQL Server vNext, SQL Server 2016, SQL Server 2015, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005
Example 1:
Let’s look at some examples of the SQL Server function CONVERT to understand how to use the CONVERT function in SQL Server (Transact-SQL). For example:
SELECT CONVERT(int, 15.49);
--Result: 15 (Result truncated)
SELECT CONVERT(float, 15.49);
--Result: 15.49 (Result not truncated)
SELECT CONVERT(varchar, 23.7);
--Result: '23.7'
SELECT CONVERT(varchar(4), 23.7);
--Result: '23.7'
SELECT CONVERT(float, '52.4');
--Result: '52.4'
SELECT CONVERT(datetime, '01.12.2017');
--Result: '2017-12-01 00:00:00.000'
SELECT CONVERT(varchar, '01.12.2017', 101);
--Result: '01.12.2017'
The main difference between the CONVERT function and the CAST function is that the former allows you to format data (e.g., datetime temporal data) when converting them to a character type and specify the format when converting them back. Different integer values of the optional style argument correspond to different types of formats. Let us consider the following example:
SELECT CONVERT(char(25), CONVERT(datetime,'20030722'));
Here we convert the datetime string view to datetime type and then perform the reverse conversion to demonstrate the formatting result. Since the style argument value is not set, the default value (0 or 100) is used. As a result, we will get:
Jul 22 2003 12:00AM
Below are some other values of the style argument and the result obtained in the above example. Note that increasing the style value by 100 results in a four-digit display of the year.
1 | 07/22/03 |
11 | 03/07/22 |
3 | 22/07/03 |
121 | 2003-07-22 00:00:00.000 |
Sql Training Online – Sql Convert Function
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...
5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...
The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...
PreambleMS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...
First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...
PreambleAtom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...
PreambleMongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...
FlexibilityOne of the most advertised features of MongoDB is its flexibility. Flexibility, however, is a double-edged sword. More flexibility means more...
PreambleSQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...
PreambleWriting an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...
What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....
PreambleIf you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...