SUBSTRING(FullName, CHARINDEX(' ', FullName) + 1, LEN(FullName)) : SUBSTRING « String Functions « SQL Server / T-SQL Tutorial






3> CREATE TABLE #MyNames (FullName VarChar(50))
4> GO
1> INSERT INTO #MyNames (FullName) SELECT 'Fred
2~ INSERT INTO #MyNames (FullName) SELECT 'Wilma
3> INSERT INTO #MyNames (FullName) SELECT 'Barney
4~ INSERT INTO #MyNames (FullName) SELECT 'Betty
5> INSERT INTO #MyNames (FullName) SELECT 'George
6~ INSERT INTO #MyNames (FullName) SELECT 'Jane
7>
8> SELECT
9> SUBSTRING(FullName, 1, CHARINDEX(' ', FullName) - 1) AS FirstName
10> , SUBSTRING(FullName, CHARINDEX(' ', FullName) + 1, LEN(FullName)) AS LastName
11> FROM #MyNames
12> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)
FirstName                                          LastName
-------------------------------------------------- --------------------------------------------------
Fred
INSERT INTO #MyNames (FullName) SELECT
Barney
INSERT INTO #MyNames (FullName) SELECT
George
INSERT INTO #MyNames (FullName) SELECT

(3 rows affected)
1>
2>
3> drop table #MyNames;
4> GO
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