I have a query such as the below:
SELECT * from table_name where lastname regexp "[[:<:]]Smith[[:>:]]"
This returns
De Smith
Smith
I only need to retrieve Smith
I even tried the below
SELECT * from surnames where last_name regexp "[[:<:]][^\s]Smith[[:>:]]"
I may be mistaking your requirement, but if you want to exactly match a last name then you can just use the equals operator:
SELECT * from table_name where TRIM(lastname) = 'Smith'
I used TRIM() on the lastname field just in case there might be leading or trailing whitespace.
Related
My table name is student and column name is FullName.
Can anyone help with this question? I have tried:
select FullName from student where fullName like "e"
But this is returning 0 rows.
If you want students that contain at leas one 'e', then:
select fullname
from student
where fullname like '%e%'
Note the use of the wildcard character (%) around the e, which searches for the character anywhere in the string.
But if you really mean students that contain a single e (not more, not less), then you need to filter out names that contain more than one. For this, you can do:
select fullname
from student
where fullname like '%e%' and fullname not like '%e%e%'
You could also use replace() and char_length():
select fullname
from student
where char_length(replace(fullname, 'e', '')) = char_length(fullname) - 1
You can use a regular expression:
select fullname
from student
where fullname regexp '^[^e]*e[^e]*$'
have that little sql select:
select * from import_daten WHERE lastname REGEXP 'dipl\.|dr\.';
And just want to filter the rows with ing. and dipl. but with that statement i also get the people wtih for e.g. "Abendroth" in Lastname. Because the "dr" in Name.
Same is with
select * from import_daten WHERE lastname REGEXP 'dipl.|dr.';
How is it possible to include the full-stop correct within the regexp?
REGEXP '(dipl|dr)[.]'
Be careful of start/end of word:
mysql> SELECT 'dr.' REGEXP 'dr[.][[:>:]]', 'dr.' REGEXP 'dr[.]';
+-----------------------------+----------------------+
| 'dr.' REGEXP 'dr[.][[:>:]]' | 'dr.' REGEXP 'dr[.]' |
+-----------------------------+----------------------+
| 0 | 1 |
+-----------------------------+----------------------+
Notice how it fails? That is because . is not a character that can exist in a 'word'.
Also, I used [.] instead of \. because of the problem of escaping the escape character -- in some situations you need \\.; in others you might need \\\\.. Too confusing.
If necessary you can use 'word start': REGEXP '[[:<:]](dipl|dr)[.]'
I think you want this
select * from import_daten WHERE lastname REGEXP '(dipl\.)|(ing\.)';
You probably want to make sure the pattern is at a "word boundary." MySQL's regular expression syntax has special character sequences for that:
select * from import_daten WHERE lastname REGEXP '[[:<:]](dipl\.|dr\.)[[:>:]]';
See http://dev.mysql.com/doc/refman/5.7/en/regexp.html. It's nearly the last item on the page before that page's user comments.
writing a select statement to view first names with spaces in them i.e. JO ANN or TERRY LYNN,
my statement format would look like:
SELECT FirstName FROM `DB`.`TABLE` where FirstName REGEXP ' '
I know the names exist because I can see them in the preview i just need to write a select statement to only view the names with spaces
It's better to use POSIX class which matches all the space characters because your's won't match the names which has tabs.
SELECT FirstName FROM `DB`.`TABLE` where FirstName REGEXP '[[:space:]]'
This would match the names only if there exists a space between two alphabets.
SELECT FirstName FROM `DB`.`TABLE` where FirstName REGEXP '[[:alpha:]][[:blank:]]+[[:alpha:]]'
How to get records which contain alphaNumeric characters + white-spaces.
OR At-list single numeric character in name.
i.e spiderman 1, abc12 part1
What I have done.:
select * from table t where t.name REGEXP '^[A-Za-z0-9]+$'
but it will gives only records which dont have white space : i.e abc123
so I also tried
select * from table t where t.name REGEXP '^[A-Za-z0-9 ]+$'
Now, it gives me some records which does not contain any numeric characters. i.e abcdefg hij
SELECT *
FROM table
WHERE name REGEXP '^[a-zA-Z0-9 ]+$' AND name REGEXP '[0-9]'
Simpler:
SELECT *
FROM table
WHERE name REGEXP '^[a-zA-Z0-9 ]*[0-9][a-zA-Z0-9 ]*$'
What about this: ^[\w\s]*$, which will match any numbers or word characters or whitespaces.
A digit may appear in the start, middle, or end. So we have to take care of these combinations too for which regex groups are used, like this:
select *
from table t
where t.name REGEXP '^[a-zA-Z ]+[0-9 ]+[a-zA-Z ]*$|^[0-9]+[a-zA-Z ]+[0-9]*$'
SELECT *
FROM customers
WHERE Firstname LIKE 'George'
The problem is that i have more than 1 rows in the table with tha name Geoge and the result of the query shows only one row
You will want to include the wildcard % character to include the rows the have George present in the name:
SELECT *
FROM customers
WHERE Firstname LIKE '%George%';
If George will always appear at the beginning, then you can include the wildcard on the end:
SELECT *
FROM customers
WHERE Firstname LIKE 'George%';
you need to add a wildcard character % to match any value that contains george
SELECT *
FROM customers
WHERE Firstname LIKE '%George%'
MySQL LIKE Operator
the statement
WHERE Firstname LIKE 'George'
is equivalent with
WHERE Firstname = 'George'
that is why you are only getting one record which firstname is george.
UPDATE 1
SQLFiddle Demo
try
LOWER(Firstname) LIKE '%george%'
handles partial values and avoids case sensietivity issues.