SQLite find words with two "a" letters - sql

I'm trying to filter out words that have two letter "a"'s in them.
I've tried using LIKE, but can't figure out how to filter words with two "a"'s (they don't have to be consecutive).
SELECT Sanat.sana FROM Sanat WHERE Sanat.sana LIKE '%a%';

For exactly 2 a's you could do:
SELECT Sanat.sana FROM Sanat WHERE LENGTH(REPLACE(Sanat.sana, 'a', '')) = LENGTH(Sanat.sana) - 2;

You would use like:
WHERE Sanat.sana LIKE '%a%a%';
Note: This will generally also match upper-case 'a's.

"LIKE '%a%a%'" would also show words with more than 2 a's.
You could instead use something like this:
SELECT sana FROM Sanat WHERE sana LIKE '%a%a%' AND sana NOT LIKE '%a%a%a%';

Related

SQL LIKE - Using square bracket (character range) matching to match an entire word

In REGEX you can do something like [a-c]+, which will match on
aaabbbccc
abcccaabc
cbccaa
b
aaaaaaaaa
In SQL LIKE it seems that one can either do the equivalent of ".*" which is "%", or [a-c]. Is it possible to use the +(at least one) quantifier in SQL to do [a-c]+?
EDIT: Just to clarify, the desired end-query would look something like
SELECT * FROM table WHERE column LIKE '[a-c]+'
which would then match on the list above, but would NOT match on e.g "xxxxxaxxxx"
As a general rule, SQL Server's LIKE patterns are much weaker than regular expressions. For your particular example, you can do:
where col not like '%[^a-c]%'
That is, the column contains no characters that are not a, b, or c.
You can use regex in SQL with combination of LIKE e.g :
SELECT * FROM Table WHERE Field LIKE '%[^a-z0-9 .]%'
This works in SQL
Or in your case
SELECT * FROM Table WHERE Field LIKE '%[^a-c]%'
I seems you want some data from database, That is you don't know exactly, You must show your column and the all character that you want in that filed.

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

Matching exactly 2 characters in string - SQL

How can i query a column with Names of people to get only the names those contain exactly 2 “a” ?
I am familiar with % symbol that's used with LIKE but that finds all names even with 1 a , when i write %a , but i need to find only those have exactly 2 characters.
Please explain - Thanks in advance
Table Name: "People"
Column Names: "Names, Age, Gender"
Assuming you're asking for two a characters search for a string with two a's but not with three.
select *
from people
where names like '%a%a%'
and name not like '%a%a%a%'
Use '_a'. '_' is a single character wildcard where '%' matches 0 or more characters.
If you need more advanced matches, use regular expressions, using REGEXP_LIKE. See Using Regular Expressions With Oracle Database.
And of course you can use other tricks as well. For instance, you can compare the length of the string with the length of the same string but with 'a's removed from it. If the difference is 2 then the string contained two 'a's. But as you can see things get ugly real soon, since length returns 'null' when a string is empty, so you have to make an exception for that, if you want to check for names that are exactly 'aa'.
select * from People
where
length(Names) - 2 = nvl(length(replace(Names, 'a', '')), 0)
Another solution is to replace everything that is not an a with nothing and check if the resulting String is exactly two characters long:
select names
from people
where length(regexp_replace(names, '[^a]', '')) = 2;
This can also be extended to deal with uppercase As:
select names
from people
where length(regexp_replace(names, '[^aA]', '')) = 2;
SQLFiddle example: http://sqlfiddle.com/#!4/09bc6
select * from People where names like '__'; also ll work

Difference between LIKE and NOT LIKE searching for alpha numeric data

What is the difference between these two statements as far as the results?
SELECT * FROM DTSEARCHER
WHERE word LIKE '%[^a-zA-Z0-9]%'
SELECT * FROM DTSEARCHER
WHERE word not LIKE '%[^a-zA-Z0-9]%'
in the first one, you will get anything that matches
in the second one, you will get the rest.

Like command SQL

I'm trying to construct an SQL query that looks for multiple different things I think the like command is the best way to achieve this
the line in the query is:
where *field* like 'AB%' it lists all the instaces in the table where it begins AB....
when i try and add multiple instances though i get an error message, I want to do something like the following:
where *field* like ('AB%','CD%','EF%')
So I get the fields for specific entries that start with the list of text I have provided
Could somebody help me with this please?
You want OR
WHERE field LIKE 'AB%'
OR field LIKE 'CD%'
OR field LIKE 'EF%'
If you are using other WHERE clause criteria, you'll need to bracket off the OR clauses using parenthesis.
Hope it helps...
EDIT:
After your comment, you could try using regular expressions, especially REGEXP_LIKE in your WHERE clause.
Something along the lines of (untested):
SELECT *
FROM table
WHERE regexp_like(field, '^(AB|CD|EF).*$')
WHERE *field* LIKE 'AB%' OR *field* LIKE 'CD%' OR *field* LIKE 'EF%'
If you are using Oracle, you can write:
where regexp_like(t1, '^AB|^CD|^EF')
etc.
You could use the instr function. If it finds the substring you want at the first position of the string, it would return 1:
where instr(field,'AB') = 1
OR instr(field,'CD') = 1
OR instr(field,'ED') = 1
If you want to do it with single string, try to use REGEXP:
WHERE *field* REGEXP 'AB(.*)|CD(.*)|EF(.*)'