Table of Contents
Convert in SQL Server Functions: CONVERT Function in SQL Server is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
You 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.
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.85SELECT CONVERT(varchar, 15.6);Result: '15.6'SELECT CONVERT(varchar(4), 15.6);Result: '15.6'SELECT CONVERT(float, '15.6');Result: 15.6SELECT CONVERT(datetime, '2019-05-02');Result: '2019-05-02 00:00:00.000'SELECT CONVERT(varchar, '05/02/2019', 101);Result: '05/02/2019'
Next lesson: TRY_CAST function in SQL Server
FAQ
What should you know about 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.
What should you know about syntax?
To use CONVERT function in SQL Server, we use the following syntax:
What is Convert in SQL Server Functions: CONVERT Function in SQL Server?
You 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.
Reader Comments 0
Sign in with email or Google to join the discussion.