The STRPOS()
function is used to determine the location in the string where the substring is being matched
STRPOS(string,substring)
How does it work?
STRPOS()
function searches the substring
in the given string
and returns the position where the substring
is found.string
: Represents the string to be searched.substring
: Represents the string whose position is to be found.The following code shows how to use the STRPOS()
function in SQL.
CREATE TABLE Employee (id int,first_name varchar(50),last_name varchar (50),salary int,gender varchar(10),state varchar(15));-- Insert dataINSERT INTO EmployeeVALUES (01,'Sharon', 'Peller',40000,'Female','Kogi');INSERT INTO EmployeeVALUES (02,'Paul', 'Dons',150000,'Male','Lagos');INSERT INTO EmployeeVALUES (03,'Ameera', 'Abedayo',200000,'Female','Imo');INSERT INTO EmployeeVALUES (04,'Maria', 'Elijah',320000,'Female','Lagos');INSERT INTO EmployeeVALUES (05,'David', 'Hassan',250000,'Male','Abuja');INSERT INTO EmployeeVALUES (06,'Niniola', 'Disu',80000,'Female','Lagos');INSERT INTO EmployeeVALUES (08,'Joe', 'Smith',75000, 'Male','Lagos');-- QuerySELECT id, first_name, gender, STRPOS(gender, 'male') AS positionFROM Employee;
In the code above:
Employee
with the columns id,
name,
level
, gender
, and state.
Employee
table.male
) is found in each cell of the gender
column using the STRPOS()
function. Finally, the result is displayed.