17

iam new to sql and i would like to know how can I find the letter or symbol at the end of value in column called Name? E.g if i would find something i will write select * from 'table' where 'Name' like '%es%' but it will find me all rows contains es
Lets say - lesl, pespe, mess... but how to write select which will select just values with 'es' At the end of word? ... using regex i will use es\Z..... thanks in advance!

2
  • Since you are new, you might want to learn some basics before you start doing real work. To that end, I've heard good things about the book, Teach Yourself SQL in Ten Minutes. Commented Mar 10, 2013 at 15:47
  • Thanks for your idea. i will find this book and will be better and better :) Commented Mar 10, 2013 at 15:54

6 Answers 6

28

You have to remove the last %, so it will only select words ending with es.

select * from table where Name like '%es'
Sign up to request clarification or add additional context in comments.

2 Comments

Thanky for your answer. It was helpful :)
Can you use the like keyword with more than one condition ?
7

You're currently matching on: ..where 'Name' like '%es%'.

Which equates to anything, then 'es' then anything else.

Removing the last % changes the where to anything then 'es'.

in short.. you need ..where 'Name' like '%es'

Comments

4

The query ..where 'Name' like '%es' will find columns where name ends with "ES". But if we need to find column where name ends with either "E" or "S", the query would be

.. where 'Name' LIKE '%[ES]'

Comments

1

You can also use REGEX

select distinct city from station where city REGEXP '[aeiou]$';

Resources: To Know more about REGEXP

Comments

0
  1. if you want to find name start with something like 'test' use => select name from table where name like 'test%'.
  2. if you want to find name end with something like 'test' use => select name from table where name like '%test'.
  3. if you want to find name start with s and end with h use => select name from table where name like 's%'and name like '%h' or simply select name from table where name like 's%h'.

Comments

0

Try pattern [a-z]ed($) with regexp operator/function.

Pattern explanation:

[a-z] - match any letter

es - match es literally

($) - end of string (so make sure it's not followed by any letters)

Full query would be:

select * from test 
where ingr regexp '[a-z]es($)'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.