CONVERT function in SQL Server
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 :
- 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.
- do_dai (optional): data type length for the result of char, varchar, nchar, nvarchar, binary and varbinary.
- 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.
- 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.
(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 :
- When converting float or numeric data types to int integers, CONVERT will cut the decimal part behind.
- See also CAST and TRY_CAST, TRY_CONVERT functions.
- 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
You should read it
- How to convert AI files to PNG, JPG without software
- Convert PDF to Word, convert PDF files for free
- LEFT function in SQL Server
- This is a very useful function in Google Sheets but not many people know it
- How to convert PNG images to PDF files
- Convert data types in C #
- PHP functions
- The oct () function in Python
May be interested
- DATENAME function in SQL Serverthe datename function in sql server returns a time value of the input argument, which can be day, month, year, quarter, hour, minute, second, millisecond ... the return value is a string type (ascii)
- ROUND function in SQL Serverthe article will give you detailed instructions on how to use the sql server round () function with the syntax and specific examples to make it easier to visualize and capture the function better.
- CEILING function in SQL Serverthe ceiling function in sql server returns the smallest integer value greater than or equal to the transmitted numeric expression.
- RIGHT function in SQL Serverthe article will explore and guide you how to use the right function in sql server to extract some characters from the right side of a given string.
- FLOOR function in SQL Serverthe floor function in sql server returns the largest integer value but less than or equal to the transmitted numeric expression.
- MONTH function in SQL Serverthis article will show you in detail how to use the datetime month () processing function in sql server with specific syntax and examples to better visualize and capture functions.
- DATEDIFF function in SQL Serverthis article will show you in detail how to use sql server's datetime datediff () function with syntax and specific examples to make it easier to visualize and capture functions.
- GETUTCDATE function in SQL Serverthis article will show you in detail how to use datetime getutcdate () function in sql server with specific syntax and examples to better visualize and capture functions.
- SQL Server YEAR functionsql server's year function returns a 4-digit integer that is the year value in the timestamp passed.
- NULLIF function in SQL Serverthis article will show you in detail how to use the nullif function handler in sql server with specific syntax and examples to better visualize and capture functions.