SQL substr Function Guide

In SQL, the substr function is used to return a specified length substring from a string. Its syntax is as follows:

substr(string, start, length)

In this case, the string is the source string from which to extract the substring, start is the starting position of the substring (counting from 1), and length is the length of the substring to be extracted.

For example, if there is a table named “name” with a column named “full_name” storing the full names of employees, to extract the last name of each employee (assuming the last name is separated by a space), you can use the following SQL statement:

SELECT substr(full_name, 1, instr(full_name, ' ') - 1) AS last_name
FROM name;

The above code extracts the characters before the first space in the full_name column, obtaining the last name of each employee.

bannerAds