Return first name : SUBSTRING « String Functions « SQL Server / T-SQL Tutorial






3> DECLARE @FullName VarChar(25)
4> SET @FullName = 'George Washington'
5> -- Return first name:
6> SELECT SUBSTRING(@FullName, 1,      CHARINDEX(' ', @FullName) - 1)
7> --               ^String    ^Start  ^Returns space index      ^Don't include space
8> GO

-------------------------
George

(1 rows affected)








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