Table of Contents
Try_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.
This guide will show you in detail how to use the TRY_CONVERT data type conversion function in SQL Server with specific syntax and examples to better visualize and capture functions.
Describe
The TRY_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, TRY_CONVERT will return NULL, otherwise it will return the corresponding conversion value.
Syntax
To use TRY_CONVERT function in SQL Server, we use the following syntax:
TRY_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 the TRY_CONVERT function.
SELECT TRY_CONVERT(int, 14.85);Result: 14(kết quả là một giá trị int bị cắt phần thập phân)SELECT TRY_CONVERT(float, 14.85);Result: 14.85(kết quả được trả về dưới dạng giá trị float và không bị cắt ngắn)SELECT TRY_CONVERT(float, '14 Main St.');Result: NULL(kết quả là NULL vì không thể chuyển đổi giá trị chuỗi này thành float)SELECT TRY_CONVERT(varchar, 15.6);Result: '15.6'(kết quả được trả về dưới dạng varchar)SELECT TRY_CONVERT(varchar(2), 15.6);Result: NULL(kết quả là NULL vì chuyển đổi không thành công do giá trị không phù hợp vớivarchar độ dài 2 ký tự)SELECT TRY_CONVERT(datetime, '2018-09-13');Result: '2018-09-13 00:00:00.000'(kết quả được trả về dưới dạng datetime)SELECT TRY_CONVERT(varchar, '2018-09-13', 101);Result: '09/13/2018'(kết quả được trả về dưới dạng varchar với kiểu 101 - mm/dd/yyyy (tiêu chuẩn Hoa Kỳ))
Next lesson: Function @@ VERSION in SQL Server
FAQ
What should you know about describe?
The TRY_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, TRY_CONVERT will return NULL, otherwise it will return the.
What should you know about syntax?
To use TRY_CONVERT function in SQL Server, we use the following syntax:
What is Try_convert Function in SQL Server?
This guide will show you in detail how to use the TRY_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.