Hàm STR – Transact-SQL – Microsoft SQL Server
Hàm STR – Transact-SQL – Microsoft SQL Server là một trong các hàm trong danh sách được cung cấp sẵn cho người sử dụng khi thao tác với hệ quản trị cơ sở dữ liệu Microsoft SQL Server.
Hàm STR – Transact-SQL – Microsoft SQL Server – Chức năng
In SQL Server (Transact-SQL), the STR function returns a string representation of a number.
Hàm STR – Transact-SQL – Microsoft SQL Server – Cú pháp
Cú pháp Hàm STR trong Transact-SQL – Microsoft SQL Server như sau :
STR( number [, length [, decimal_places ] ] )
Tham số
- number
- The numeric value to convert to a string.
- length
- Optional. The length of the resulting string which includes all digits, decimals, signs, etc. If length is not specified, it will default to 10.
- decimal_places
- Optional. The number of decimal places to display in the resulting string and can not exceed 16. If decimal_places is not specified, it will default to 0.
Trong quá trình sử dụng hàm STR trong Transact-SQL – Microsoft SQL Server hãy nhớ :
Hàm STR – Transact-SQL – Microsoft SQL Server – Chú ý
- The STR function will round the result if there isn’t enough length or decimal_places to display the resulting string based on the parameters provided.
Hàm STR – Transact-SQL – Microsoft SQL Server – Phạm vi
Hàm STR có thể sẵn sàng sử dụng trong Transact-SQL – Microsoft SQL Server với các phiên bản Microsoft SQL Server như sau:
SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005
Hàm STR – Transact-SQL – Microsoft SQL Server – Ví dụ
Dưới đây là các ví dụ minh họa cách sử dụng hàm STR trong Transact-SQL – Microsoft SQL Server như sau:
Ví dụ:
SELECT STR(123); Result: '123' SELECT STR(123.5); Result: '124' (result is rounded because decimal places defaults to 0) SELECT STR(123.5, 5); Result: '124' (result is rounded because decimal places defaults to 0) SELECT STR(123.5, 5, 1); Result: '123.5' SELECT STR(123.456, 7, 3); Result: '123.456' SELECT STR(123.456, 7, 2); Result: '123.46' (result is rounded because decimal places is set to 2) SELECT STR(123.456, 7, 1); Result: '123.5' (result is rounded because decimal places is set to 1) SELECT STR(123.456, 7, 0); Result: '123' (result is rounded because decimal places is set to 0) SELECT STR(123.456, 7); Result: '123' (result is rounded because decimal places defaults to 0)