Reputation: 45
I tried to select the name of students that end with 'a'. I wrote this code:
Select name form students where name like '%a' ;
How can I get the same results using SUBSTR
?
Upvotes: 1
Views: 955
Reputation: 521997
I actually think using RIGHT()
would make the most sense here:
SELECT name
FROM students
WHERE RIGHT(name, 1) = 'a'
The above query would work on MySQL, SQL Server, and Postgres, but not Oracle, where you would have to use SUBSTR()
:
SELECT name
FROM students
WHERE SUBSTR(name , -1) = 'a'
Upvotes: 5
Reputation: 514
You can use :
Select name from students where SUBSTR(name, -1, 1) = 'a' ;
Upvotes: 1
Reputation: 6749
Not all platforms accept negative start integers or length integers for SUBSTR()
Can you try if your DBMS supports the RIGHT() string function?
Works like this:
SQL>SELECT RIGHT('abcd',1) AS rightmost_char;
rightmost_char
--------------
d
Happy playing ...
Marco
Upvotes: 1
Reputation: 15061
Using SUBSTR()
.
SELECT name
FROM students
WHERE SUBSTR(name , -1) = 'a'
Upvotes: 0