SQL - how to select words with certain values at the end of word - sql

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!

You have to remove the last %, so it will only select words ending with es.
select * from table where Name like '%es'

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'

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]'

You can also use REGEX
select distinct city from station where city REGEXP '[aeiou]$';
Resources: To Know more about REGEXP

if you want to find name start with something like 'test'
use => select name from table where name like 'test%'.
if you want to find name end with something like 'test'
use => select name from table where name like '%test'.
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'.

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($)'

Related

Find the last word of a sentence SQL

I'm trying to return a table that contains all the employees who do have the first name starting with an 'A' and the surname starting with a 'R', from the tables 'dep' and 'emp'. I tried to use the INDEXOF function but seems it doesn't work . what can I do?
select emp.nome,emp.sal,emp.ndep,dep.nome,
from emp, dep
where (substring(emp.nome,1,indexof(' ',emp.nome)))like 'A%'
and (substring(emp.nome,lastindexof(' ',emp.nome),emp.nome.lenght)) like 'R%'
order by 1
There's no point doing the first substring, as a string 'ABC DEF' is like 'A%' whether or not you cut the bit off after the space.
The surname, your use of lastindexof causes the space to become part of the name because you forgot to add 1 to skip onto the next character after the space, and ' RST' is never like 'R%'. I swapped your lastindexof to instr, because I wasn't sure if lastindexof is as universally applicable as instr. calling instr with a negative start index causes it to search backward from the end of the string
select emp.nome,emp.sal,emp.ndep,dep.nome,
from
emp
INNER JOIN dep ON /*something_goes_here*/
where emp.nome like 'A%' and
SUBSTR(emp.nome, INSTR(emp.nome, ' ', -1) + 1,emp.nome.length)) like 'R%'
order by 1
You need to finish the query off by putting a clause in describing how the tables should be joined
I would expect a condition like this:
where nom like 'A% R%'
assuming that nom has both the first name and last name.

List student where second character of name is 'a' in oracle

This is my table STUDENT:
11 sourav 01-JUN-10
12 kamal 01-JUN-10
13 RAHUL 01-JUN-10
14 SOVAN 01-DEC-10
15 SHAYMAL 01-DEC-10
Iam trying to find names, whose second character is an 'A'. I wrote the following SELECT for it:
SELECT
name
FROM
student
WHERE
REGEXP_LIKE(name,'[a-z]a.*','i');
It is not working though. Can someone tell my why?
LIKE is better than REGEXP_LIKE() for this purpose:
where name like '_a%'
If you need case insensitivity, you can do:
where lower(name) like '_a%'
LIKE patterns always start matching at the beginning of the string.
As a regular expression, you can do:
where regexp_like(name, '^.a')
The ^ anchors the regular expression to the beginning of the string. The . matches any single character.
If you want this case-insensitive, you can use:
where regexp_like(name, '^.[aA]')
There are many ways you could solve this including:
SELECT NAME
FROM STUDENT
WHERE LOWER( NAME ) LIKE '_a%';
or
SELECT NAME
FROM STUDENT
WHERE NAME LIKE '_a%'
OR NAME LIKE '_A%';
or
SELECT NAME
FROM STUDENT
WHERE SUBSTR( LOWER( NAME ), 2, 1 ) = 'a';
or
SELECT NAME
FROM STUDENT
WHERE INSTR( LOWER( NAME ), 'a', 2 ) = 2;
or
SELECT NAME
FROM STUDENT
WHERE REGEXP_LIKE( NAME, '^.a', 'i' );
It is not working though. Can someone tell my why?
The regular expression '[a-z]a.*' is not anchored to the start of the string so it will match and alphabetic character followed by an a character anywhere in the string. If you want to find only the second character then you need to start the regular expression with ^ which matches the start-of-the-string.
WHERE REGEXP_LIKE( NAME, '^[a-z]a', 'i' );
You do not need to match the rest of the string so appending .* is redundant.
Also, only matching [a-z] as the first character is likely to work for most of the names you will encounter that match this pattern but it will not match names with: an accented first character; or a character in an extended character-set first; or with a number or punctuation as the first character. You could make the regular expression more permissive by using '^.a' as the regular expression.
I am guessing that you want a case insensitive comparison, so you would need something like the following:
SELECT NAME
FROM STUDENT
WHERE UPPER(NAME) LIKE '_A%';
SELECT NAME
FROM STUDENT
WHERE REGEXP_LIKE(NAME,'^[a-z]a.*','i');
OR
SELECT NAME
FROM STUDENT
WHERE LOWER(SUBSTR(NAME,2,1)) ='a'
It simple.
Use regexp_like Operator
SELECT NAME
FROM STUDENT
WHERE regexp_like(NAME, '^.a');
There are a lot of ways to solve this.
The simplest one is where name like '_a%' or name like '_A%'

Find substring in string

Is it possible to check if a specific substring which is in SQL Server column, is contained in a user provided string?
Example :
SELECT * FROM Table WHERE 'random words to check, which are in a string' CONTAINS Column
From my understanding, CONTAINS can't do such kind of search.
EDIT :
I have a fully indexed text and would like to search (by the fastest method) if a string provided by me contains words that are present in a column.
You can use LIKE:
SELECT * FROM YourTable t
WHERE 'random words ....' LIKE '%' + t.column + '%'
Or
SELECT * FROM YourTable t
WHERE t.column LIKE '%random words ....%'
Depends what did you mean, first one select the records that the column has a part of the provided string. The second one is the opposite.
Just use the LIKE syntax together with % around the string you are looking for:
SELECT
*
FROM
table
WHERE
Column LIKE '%some random string%'
This will return all rows in the table table in which the column Column contains the text "some random string".
1) If you want to get data starting with some letter you can use % this operator like this in your where clause
WHERE
Column LIKE "%some random string"
2) If you want to get data contains any letter you can use
WHERE
Column LIKE "%some random string%"
3)if you want to get data ending with some letter you can use
WHERE
Column LIKE "some random string%"

SQL Find names that contain a letter (without using Like)

I need to write a select statement that returns all last names from a column that contains the letter A. I can't use LIKE. I am trying to do so with SUBSTR.
I don't think substr is the way to go. instr, on the other hand, may do the trick:
SELECT last_name
FROM mytable
WHERE INSTR(last_name, 'A') > 0
EDIT:
As David Bachmann Jeppesen mentioned, Oracle is case sensitive, so if you want to find last names containing any case of "A", you could do something like this:
SELECT last_name
FROM mytable
WHERE INSTR(UPPER(last_name), 'A') > 0

SQL - search by beginning of a word

I want to write an SQL SERVER statement that searches for the beginning of a word in a string that starts with something.
For example, if I search for 'em' on the Company record, I should get:
Emily, Inc
The Emmmy
NOT
Forget Them
Lemming, LLC
I can do this in PHP by extracting/slicing the string into an array and searching the beginning of each words.
But how I would write this query in SQL server without resorting to Stored procedures/functions?
JW's answer will work for entries where Em is at the very beginning of the field.
If you also need to retrieve values where the word is not the first in the string, try:
SELECT * FROM tableName WHERE CompanyName LIKE 'Em%' or CompanyName LIKE '% Em%'
(This is assuming all word are separated by a space.)
use LIKE
SELECT * FROM tableName WHERE CompanyName LIKE 'Em%'
Another option is CONTAINS, something like:
SELECT ... WHERE CONTAINS(CompanyName, 'Em*');
For MS Access:
SELECT * FROM Table1 WHERE Company_Name LIKE 'Word*'
For more standard DBMSs:
SELECT * FROM Table1 WHERE Company_Name LIKE 'Word%'