Mayukh Nair
Mayukh Nair

Reputation: 653

search in MySQL database based on alphabet's position?

I want to search in my database "emp" for names which have the alphabet "A" as third alphabet from left in their names.

This is what I tried:

select empname from emp
where left(empname,3)="a"
order by empname;

It returns an empty set. Can you check my syntax, whether it is correct or not!

(Upvote promised ;) )

Upvotes: 0

Views: 740

Answers (2)

Minhaj T
Minhaj T

Reputation: 89

The INSTR() function returns the position of the first occurrence of a string in another string. This function performs a case-insensitive search.

Select INSTR(first_name, 'm') from Tablename where first_name = 'monika';

Reference:- https://www.w3schools.com/sql/func_mysql_instr.asp

Upvotes: 0

user1864610
user1864610

Reputation:

left(empname,3) will return the three leftmost characters in the string.

You need substring(empname,3,1), which will start at the third character and return a string of length 1.

The reference is here

Your query becomes:

select empname from emp
  where substring(empname,3,1)="a"
  order by empname;

Upvotes: 3

Related Questions