Show names with 3 words only in SQL Server database - sql

I have a database with table contains information about employees in my company.
I need a SQL statement to show all names whose have 3 parts like "haitham samey hegazy" and who less or more than 3 parts didn't shown in the condition result.
I try to use like operator but it doesn't work and my database is in Arabic language.
Thanks for helping...

Assuming names are separated by a single space, you can get at least three parts using:
where name like '% % %'
You can get exactly three parts using:
where name like '% % %' and name not like '% % % %'

You can use replace to count the number of spaces:
SELECT *
FROM Table
WHERE LEN(LTRIM(RTRIM(varcharColumn))) -
LEN(REPLACE(LTRIM(RTRIM(varcharColumn)), ' ', '')) >= 2
This will return all the rows where there are at least 2 spaces inside the varcharColumn.

Related

WHERE colname LIKE '% %' OR colname LIKE '% % %' is returning rows with four words

The following code is supposed to return rows where the City name is two or three words, however it also returns one with four words:
SELECT FirstName ||' '|| LastName AS 'Full Name', City, Country, Email, Phone
FROM customers
WHERE City LIKE '% %' OR City LIKE '% % %';
[Output from Query]
São José dos Campos appears if I use any of the three following codes:
LIKE '% %'
LIKE '% % %'
LIKE '% % % %'
Why is this happening?
Other answers have explained why you're getting the answer you are -- the % wildcard with LIKE matches space characters, too -- but nobody has explained how to get what you want.
This will do it:
WHERE LENGTH(City) - LENGTH(REPLACE(City, ' ', '')) IN (1,2)
Syntax will vary somewhat by RDBMS (e.g., SQL Server calls it the LEN() function).
What this does is take the City field and replace each space with an empty string, which effectively removes that character. Then it subtracts the length from the length of the City with the spaces in it. That tells you how many spaces are in the City field. Then you just look for City fields that have the right number of spaces. 1 space means two words, etc.
Beware of leading or trailing spaces in some RDBMSs, as well as consecutive spaces.
If you need to use the LIKE operator, you can try something like this:
WHERE City LIKE '% %'
AND City NOT LIKE '% % % %'
That will return where the City has at least one space, but less than three or more. Here, were using the fact that the 1 space pattern matches 2 spaces and the 3 space pattern matches 4 spaces to our advantage! You'll still want to beware of consecutive spaces and leading or trailing spaces. That may create unexpected results.
You'll have to test to see which option performs better in your system.
Some RDBMSs will give you expanded LIKE or full regex options, which can work even better than what's above.
The wildcard % matches anything including spaces. In particular a pattern like '% %' will match 'São José dos Campos' because:
The first % matches "São José dos" and the second % matches "Campos", or
The first % matches "São José" and the second % matches "dos Campos", or
The first % matches "São" and the second % matches "José dos Campos".
You don't mention which database you are using, but most database engines, now offer regular expression matching. With those you can precisely search for a very specific pattern.
Symbol % means anything, including any number of spaces.
It happens because "who can do the most, can do the least".
When using LIKE statement, % doesn't mean "a word" but "any pattern". Therefore, % % doesn't mean "two words separated by a space", but "any pattern, followed by a space, followed by any pattern". This actually makes you look for a string that contains at least one space.
And here's the cause: if a string that contains four spaces, it does contains two of them. So looking for this kind of string actually matches every string that contains two or more spaces, thus matching two, three, four or more words.
Perhaps you meant
SELECT FirstName ||' '|| LastName AS 'Full Name', City, Country, Email, Phone
FROM customers
WHERE City LIKE '% %'
and City not LIKE '% % % %'
This means there needs to be at least one space but not 3 or more spaces.
In SQL, % is zero, one, or many characters, including spaces. So Cities with one, two or more spaces between those cities will match in LIKE "% %".
You can try this if you want cities that have only one or two spaces within:
WHERE City LIKE '% %' AND City NOT LIKE '% % % %';
But if there are cities that have 4 space or 5 space, you need to add another AND ... NOT LIKE for that.

Find white-space at the front in SQL data

Thank you for your help.
How do I get data that contains a space at the front or the back?
For example: (see .png)
enter image description here
I tried:
select* from customer_table where customer like '% %'
but:
it gave me all of them because name contains a space in between.
I need to get the ones that have a white-space at the front.
thank you!
I think you want:
where customer like ' %' or customer like '% '
like patterns match the entire string. So the first gets spaces at the begging and the second spaces at the end. If you wanted both, you can do that in a single patter:
where customer like ' % '
Use TRIM function on the column to fit you select condition, for instance
WHERE LTRIM([CUSTOMER]) = 'Joe'
This example is for Microsoft SQL Server but I believe all DBMS have a similar function you can use trim as the keyword to search.

SQL LIKE Statement with Spaces

Spaces in my phone numbers are causing issues when I am trying to search.
Select * from customers where number LIKE '%02722231%'
This will return records that're LIKE '02722231', but this will not return any records that contain a space e.g. '027 22231'
Can this be done with regular expressions? I need to search 0272542155 and get all records the same including 027 2542155
Try this:
Select * from customers where REPLACE(number, ' ', '') LIKE '%02722231%'

how to make a select query to match a word with an unknown suffix?

For this query:
SELECT * FROM TABLE WHERE name LIKE '% testt %' OR name LIKE '% tests %';
Tell me please how make select where will be search word test + one unknown symbol on end word?
example rows:
Hello tests Words
Hello tested Words
Hello testing Words
result should get row Hello tests Words
Use the _ symbol. It matches any ONE character, where as % matches any length of symbols.
WHERE name LIKE '% test_ %'

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