Returning a Chunk of an Expression : SUBSTRING « String Functions « SQL Server / T-SQL Tutorial






4>
5> DECLARE @BankAccountNumber char(14)
6> SET @BankAccountNumber = '1111-1111-1111'
7> SELECT 'XXXX-' + SUBSTRING(@BankAccountNumber, 6,4) + '-XXXX' Masked_BankAccountNumber
8> GO
Masked_BankAccountNumber
------------------------
XXXX-1111-XXXX

(1 rows affected)
1>








12.22.SUBSTRING
12.22.1.SUBSTRING is used to retrieve part of a string from another string. The syntax for the function is as follows:
12.22.2.Returning a Chunk of an Expression
12.22.3.Substring with variable
12.22.4.SUBSTRING(FullName, CHARINDEX(' ', FullName) + 1, LEN(FullName))
12.22.5.select SUBSTRING('(559) 555-1212', 7, 8)
12.22.6.Return first name
12.22.7.Get the last name
12.22.8.A SELECT statement that uses the LEFT, RIGHT, and SUBSTRING functions