SQL Server String Functions
SQL Server string functions are scalar functions that perform an operation on a string input value and return a string or numeric value.
SUBSTRING Function
SUBSTRING function retrieves a portion of the string starting at the specified character and bringing back the number of characters specified; the syntax is:
SUBSTRING(string, starting_character_number, number_of_characters_to_return)
The following example will retrieve four characters from the employee last names, starting at the third character:
SELECT SUBSTRING(LastName, 3, 4) AS PortionOfLastName FROM DimEmployee
Results:
PortionOfLastName FullLastName
lber Gilbert
mbur Tamburello
lter Walters
lter Walters
Notice that the SUBSTRING function finds the starting character by counting from the left. In other words, if we run SUBSTRING(LastName, 3, 4) against the last name of "Buchanan" we start on the 3rd character from the left - "c".
Tags:
SQL Server string functions are scalar functions that perform an operation on a string input value and return a string or numeric value.
SUBSTRING Function
SUBSTRING function retrieves a portion of the string starting at the specified character and bringing back the number of characters specified; the syntax is:
SUBSTRING(string, starting_character_number, number_of_characters_to_return)
The following example will retrieve four characters from the employee last names, starting at the third character:
SELECT SUBSTRING(LastName, 3, 4) AS PortionOfLastName FROM DimEmployee
Results:
PortionOfLastName FullLastName
lber Gilbert
mbur Tamburello
lter Walters
lter Walters
Notice that the SUBSTRING function finds the starting character by counting from the left. In other words, if we run SUBSTRING(LastName, 3, 4) against the last name of "Buchanan" we start on the 3rd character from the left - "c".