CONVERT function in SQL Server

The CONVERT function in SQL Server allows you to convert an expression to any desired data type but can follow a certain format (especially for the date data type).

This article will show you in detail how to use the CONVERT data type conversion function in SQL Server with specific syntax and examples to better visualize and capture functions.

Describe

The CONVERT function in SQL Server allows you to convert an expression to any desired data type but can follow a certain format (especially for the date data type). If the conversion fails, CONVERT will report an error, otherwise it will return the corresponding conversion value.

Syntax

To use CONVERT function in SQL Server, we use the following syntax:

 CONVERT (kieudulieu (do_dai), bieuthuc, dinh_dang) 

Parameters :

  1. kieudulieu: the new data type name to which the expression will be converted. Probably 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.
  2. do_dai (optional): data type length for the result of char, varchar, nchar, nvarchar, binary and varbinary.
  3. bieuthuc: value to convert to another data type, can also be the name of a column in the table or a calculation expression that needs to be converted to a new data type.
  4. dinh_dang (optional): is a number that specifies the format for converting data from date format to string form. The table below describes some common formats used in CONVERT.
Year format
(yy) Year format
(yyyy) Display data 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-ddyy (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 clock) 14 114 hh: mi: ss: mmm (24 hour clock) 20 120 yyyy-mm-dd hh: mi: ss (ODBC canonical - 24 hour clock) 21 121 yyyy-mm-dd hh : mi: ss: mmm (ODBC canonical - 24 hour clock) 126 yyyy-mm-ddThh: mi: ss: mmm (ISO8601 standard) 127 yyyy-mm-ddThh: mi: ss: mmmZ (ISO8601 standard) 130 dd y yyyy hh: mi: ss: mmmAM / PM (Hijri standard) 131 dd / mm / yy hh: mi: ss: mmmAM / PM (Hijri standard)

Note :

  1. When converting float or numeric data types to int integers, CONVERT will cut the decimal part behind.
  2. See also CAST and TRY_CAST, TRY_CONVERT functions.
  3. CONVERT can be used in later versions of SQL Server: SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, and SQL Server 2005.

For example

Take a look and explore some examples of CONVERT functions in SQL Server.

 SELECT CONVERT(int, 14.85); 
Result: 14 (kết quả cắt phần thập phân phía sau)

SELECT CONVERT(float, 14.85);
Result: 14.85

SELECT CONVERT(varchar, 15.6);
Result: '15.6'

SELECT CONVERT(varchar(4), 15.6);
Result: '15.6'

SELECT CONVERT(float, '15.6');
Result: 15.6

SELECT CONVERT(datetime, '2019-05-02');
Result: '2019-05-02 00:00:00.000'

SELECT CONVERT(varchar, '05/02/2019', 101);
Result: '05/02/2019'

Previous article: CAST function in SQL Server

Next lesson: TRY_CAST function in SQL Server

4 ★ | 1 Vote