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.