Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

CONVERT – SQL Server function

19 June 2020

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
0100mon dd yyyy hh:miAM/PM (default)
1101mm/dd/yyyy (US standard)
2102yy.mm.dd (ANSI standard)
3103dd/mm/yy (British/French standard)
4104dd.mm.yy (German standard)
5105dd-mm-yy (Italian standard)
6106dd mon yy
7107Mon dd, yy
8108hh:mi:ss
9109mon dd yyyy hh:mi:ss:mmmAM/PM
10110mm-dd-yy (USA standard)
11111yy/mm/dd (Japan standard)
12112yymmdd (ISO standard)
13113dd mon yyyy hh:mi:ss:mmm (Europe standard — 24-hour)
14114hh:mi:ss:mmm (24-hour)
20120yyyy-mm-dd hh:mi:ss (ODBC canonical — 24-hour)
21121yyyy-mm-dd hh:mi:ss:mmm (ODBC canonical — 24-hour)
126yyyy-mm-ddThh:mi:ss:mmm (ISO8601 standard)
127yyyy-mm-ddThh:mi:ss:mmmZ (ISO8601 standard)
130dd mon yyyy hh:mi:ss:mmmAM/PM (Hijri standard)
131dd/mm/yy hh:mi:ss:mmmAM/PM (Hijri standard)

Converting float into real

MeaningExplanation
0Max. 6 digits (default)
18 digits
216 digits

Converting money into character

MeaningExplanation
0Separator not comma, 2 digits to the right of the decimal (i.e. 1234.56)
1Comma separator, 2 digits to the right of the decimal (i.e.: 1,234.56)
2Separator 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.

107/22/03
1103/07/22
322/07/03
1212003-07-22 00:00:00.000

Sql Training Online – Sql Convert Function

 
Tags: , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB 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...

Preamble​​MS 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...

Preamble​​Atom 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...

Preamble​​MongoDB 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...

Preamble​​SQLShell 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...

Preamble​​Writing 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...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM 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....

Preamble​​If 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...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...