I'm trying to fetch all instances where the 1st letter of a person's first name is equal to P.
This is what I came up with, which doesn't return anything:
$sql="SELECT * FROM people WHERE SUBSTRING(FirstName,0,1) = 'P'";
Suggestions?
SUBSTRING() : function in MySQL is used to derive substring from any given string . It extracts a string with a specified length, starting from a given location in an input string. The purpose of substring is to return a specific portion of the string.
The SUBSTRING SQL function is very useful when you want to make sure that the string values returned from a query will be restricted to a certain length. In the following example, using the 'firstname' column, the last two characters are matched with the word 'on' using the SQL SUBSTRING function in the where clause.
The reason your expression doesn't work is that substring() positions are 1-based
Try either of these:
where FirstName like 'P%'
or
where substring(FirstName,1,1) = 'P'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With