Query to select strings end with certain character - mysql

I have a column that contains strings as:
aaa_1
aaa_11
I need to query strings that ends with _1. I tried the following:
select col from table where col like %_1;
But the query gives me the strings that ends with _1 and _11. How can I correct this ?

Try this:
select col from table where col like '%\_1'
character _ is a jolly, like %, but it matches only a single character, so you have to escape it with \
See here: http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like

You'll want to use something more substring related.
Try a where clause like:
SELECT col from table WHERE RIGHT(col, 2) = '_1'

You should escape % and _ by adding backslash \ as they are wildcards in mysql:
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
String | Description
\% | Matches one “%” character
\_ | Matches one “_” character

Many of these will pick up things like %_12 or %_111121, etc. Make sure you test out various cases to be sure these SELECT statements are giving you the proper subset.

SELECT col FROM table WHERE col REGEXP '_1[[:>:]]'

You can make a quick use of this query to filter out strings ending with specific character(s).
The below query output will give all names ending with 'er'.
select column_name
from table
where column_name regexp '.*er$';

Related

How do I select all(*) in a row where a column eg. PHONE ends with certain intergers? [duplicate]

I have a column that contains strings as:
aaa_1
aaa_11
I need to query strings that ends with _1. I tried the following:
select col from table where col like %_1;
But the query gives me the strings that ends with _1 and _11. How can I correct this ?
Try this:
select col from table where col like '%\_1'
character _ is a jolly, like %, but it matches only a single character, so you have to escape it with \
See here: http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like
You'll want to use something more substring related.
Try a where clause like:
SELECT col from table WHERE RIGHT(col, 2) = '_1'
You should escape % and _ by adding backslash \ as they are wildcards in mysql:
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
String | Description
\% | Matches one “%” character
\_ | Matches one “_” character
Many of these will pick up things like %_12 or %_111121, etc. Make sure you test out various cases to be sure these SELECT statements are giving you the proper subset.
SELECT col FROM table WHERE col REGEXP '_1[[:>:]]'
You can make a quick use of this query to filter out strings ending with specific character(s).
The below query output will give all names ending with 'er'.
select column_name
from table
where column_name regexp '.*er$';

MySQL REGEXP - Select certain pattern of numbers and characters

Anyone have a clue how I could go about trying to select a certain pattern of numbers with a 1 at the end?
Ex.
SELECT pattern FROM table WHERE pattern REGEXP '1_2+2_2+3_2+4_2&2016-06-09&1';
or
SELECT pattern FROM table WHERE pattern REGEXP '2_1&2016-06-09&1';
using the same number-underscore-number, ampersand, date, ampersand, number; just as long as that number 1 is at the end?
EDIT:
Actually, let me phrase it better. How do I use REGEXP to select an ampersand and the number 1 at the end of a string?
You don't need regex. Just use LIKE:
LIKE '%&1'
The % makes it not be anchored to the start of the string. LIKE is not regex, but closer to a glob syntax. It may be faster than regex, too.
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
SELECT column_name
FROM table
WHERE column_name LIKE '%&1';
Note:
You can also use LIKE operator for searching from start not only from end.
Here is an Example.
SELECT column_name
FROM table
WHERE column_name LIKE '&1%';

Show/convert only alphanumeric data in sql query [duplicate]

I'm trying to select all rows that contain only alphanumeric characters in MySQL using:
SELECT * FROM table WHERE column REGEXP '[A-Za-z0-9]';
However, it's returning all rows, regardless of the fact that they contain non-alphanumeric characters.
Try this code:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$'
This makes sure that all characters match.
Your statement matches any string that contains a letter or digit anywhere, even if it contains other non-alphanumeric characters. Try this:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$';
^ and $ require the entire string to match rather than just any portion of it, and + looks for 1 or more alphanumberic characters.
You could also use a named character class if you prefer:
SELECT * FROM table WHERE column REGEXP '^[[:alnum:]]+$';
Try this:
REGEXP '^[a-z0-9]+$'
As regexp is not case sensitive except for binary fields.
There is also this:
select m from table where not regexp_like(m, '^[0-9]\d+$')
which selects the rows that contains characters from the column you want (which is m in the example but you can change).
Most of the combinations don't work properly in Oracle platforms but this does. Sharing for future reference.
Try this
select count(*) from table where cast(col as double) is null;
Change the REGEXP to Like
SELECT * FROM table_name WHERE column_name like '%[^a-zA-Z0-9]%'
this one works fine

Mysql SELECT all rows where char exists in value but not the last one

I need a SELECT query in MYSQL that will retrieve all rows in one table witch field values contain "?" char with one condition: the char is not the last character
Example:
ID Field
1 123??see
2 12?
3 45??78??
Returning rows would then be those from ID 1 and 3 that match the condition given
The only statement I have is:
SELECT *
FROM table
WHERE Field LIKE '%?%'
But, the MySQL query does not solve my problem..
The LIKE expressions also support a wildcard "_" which matches exactly one character.
So you can write an expression like the example below, and know that your "?" will not be the last character in the string. There must be at least one more character.
WHERE intrebare LIKE '%?_%'
Re comment from #JohnRuddell,
Yes, that's true, this will match the string "??" because a "?" exists in a position that is not the last character.
It depends whether the OP means for that to be a match or not. The OP says the string "45??78??" is a match, but it's not clear if they would intend that "4578??" to be a match.
An alternative is to use a regular expression, but this is a little more tricky because you have to escape a literal "?", so it won't be interpreted as a regexp metacharacter. Then also escape the escape character.
WHERE intrebare REGEXP '\\?[^?]'
you can just add an additional where where the last character is not a ?
SELECT *
FROM intrebari
WHERE intrebare LIKE '%?%' AND intrebare NOT LIKE '%?'
you could also do it like this
SELECT *
FROM intrebari
WHERE intrebare LIKE '%?%' AND RIGHT(intrebare,1) <> '?'
DEMO

search those records which has column value contains % sign in mysql

I want to fetch all records which has one column contained % sign in mysql
we can do this using mysql using like
for ex..
select * from table where column like '%%';
it returns all records..
Please suggest
Use a backslash to escape the percent:
select * from table where column like '%\%%';
will match any row containing a percent character
% is a special character, try using escape characters to find it. Right now you're just telling mysql to look for a string using 2 wildcard characters (%) as opposed to the actual '%' character. Try using
select * from table where column = 'a%' ESCAPE 'a'
Basically telling mySQL to "Look for the string 'a%', but remove the char a in front of it.
EDIT: Another option is just using
select * from table where column = '\%'
Doing the same thing on later mySQL versions. The backslash is the "standard" escape character.
EDIT 2: Or to actually answer your question:
select * from table where column = '%\%%'
You need to escape the literal % sign with a \ e.g.
select * from table where column like '\%%';