The SUBSTRING() function in SQL is used to extracts characters from a string.
Syntax : SUBSTRING(string, start_index, length_of_character)
Above Syntax string parameter is the string passed for extraction, start_index is the index where the extraction starts length_of_character is the length of characters to extract from string.
start index of extraction for all string in SQL starting index is always 1.
If you pass negative number in length parameter it will throw and error.
SQL Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /* ............... START ............... */ ------------------------------------------------------- SELECT SUBSTRING('SQL Substring example', 1, 3) result; result ------ SQL (1 row affected) ------------------------------------------------------- SELECT SUBSTRING('SQL Substring example', 5, 9) result; result ------ Substring (1 row affected) -------------------------------------------------------- SELECT SUBSTRING(ClientName, 2, 5) AS Name FROM Client; /* This will extract the substring from the text in a column ClientName (start at position 2, extract 5 characters) */ -------------------------------------------------------- SELECT SUBSTRING("SQL Tutorial", -5, 5) AS ExtractString; /* Extract a substring from a string (start from the end, at position -5, extract 5 characters) */ /* ............... END ............... */ |