SUBSTRING() Function with Literal Strings
Using SQL SUBSTRING function with literal strings is very easy, just put the desired values in the syntax.
SUBSTRING() Function with Literal Strings Example
Consider this SQL SUBSTRING function command:
SELECT SUBSTRING ('SQL In w3wiki', 7, 18) AS ExtractString
It will take the original string ‘SQL In w3wiki’ and extract a substring beginning with the 7th character and extracting a length of 18 characters. A new column with the alias ExtractString will be returned along with the resulting substring.
To extract a section of a string based on a predetermined starting position and length, the SUBSTRING function is utilized. The original string is passed as the function’s first argument, and the second argument specifies the starting character, in this case, the seventh one. The third argument, which is 18 characters in this case, specifies how long the substring is to be extracted.
The purpose of using this SQL query is to extract a specific part of a longer string that is needed for further analysis or reporting. It can be useful when dealing with large datasets where we need to extract and manipulate specific information from a long string of text.
Output
SQL Server SUBSTRING() Function
SUBSTRING function in SQL Server is used to extract a substring from a string, starting at a specified position and with an optional length.
It is very useful when you need to extract a specific portion of a string for further processing or analysis.
SQL SUBSTRING function also works in Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse.