mysql search string and numbers like #654 - mysql

I have a quick question how can I search #some number in mysql> For example
Select * from table where field contains #numbers?
where #numbers represent any number(example: #123, #345, ...) which has more then four characters. How should I do it correctly?
EDIT: I get what I need with this
Select * from table where field REGEXP '^#+[0-9]'
Is that ok? And I also need to be at least three characters long

If you need the numbers to be at least 3 digits long, try:
SELECT * FROM table WHERE field RLIKE '#[0-9]{3,}'

If I have good understood you that code should solve your problem : SELCET * FROM table WHERE field LIKE '%#%s'

SELECT * FROM table WHERE field RLIKE '#^[0-9]'

SELECT * FROM table WHERE fields REGEXP '^#[0-9]{3}'
If i understand you..

SELECT *
FROM table
WHERE field LIKE '%#[0-9][0-9][0-9]%'

Related

SQL SELECT WHERE NOT LIKE

I have an array:
$codes = array (97121, 97122, 97123, 97180);
The real array is much longer, it contains nearly 130 codes so I think it doesn't have to be a loop.
And I have a mySQL table that has a column VilleAnn (city with postal code) ex:
Sainte-Anne 97180
Sanary-sur-Mer 83110
Using mysqli I have to select where VilleAnn doesn't contain any postal code from the array $codes.
If the VilleAnn is a simple postal code I would say:
SELECT * FROM table WHERE VilleAnn NOT IN(97121, 97122, 97123, 97180)
But in this case it must be something using NOT LIKE "%..."
Any suggestions?
You could you REGEXP:
SELECT * FROM table WHERE VilleAnn NOT REGEXP '97121|97122|97123|97180|....'
In condition doesn't work with the wildcard characters. Your best bet is to try and extract the postcode from the original field, in this example
SELECT * FROM table WHERE right(VilleAnn,5) NOT IN ('97121', '97122', '97123', '97180')
I presume real life is more complicated, so this might need to be adjusted to reflect the actual format of the field.
Something like this:
SELECT *
FROM `table`
WHERE (
`VilleAnn` NOT LIKE '%97121%' AND
`VilleAnn` NOT LIKE '%97122%' AND
`VilleAnn` NOT LIKE '%97123%' AND
`VilleAnn` NOT LIKE '%97180%'
)
SELECT * FROM table WHERE VilleAnn NOT REGEXP '[0-9]';
This will help you.
For to check four times occurrences you can use:
SELECT * FROM table WHERE VilleAnn NOT REGEXP '[0-9]{4}';

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

Query to select data from mysql with specific condition

In MySQL we can use this code to select rows with ID numbers (or anything else) between a list:
SELECT * FROM TABLENAME WHERE prophrases IN (1,2,3,4,5,6)
Thats OK! but if we need to search a number in a Field's value, what we can do?
For example, I have a table with a field, named 'prophases' and I saved data like this:
rowid / prophases
1 / 1,2,3,4,5,6,7,8
2 / 6,5,2,7,9,2
now, i need to check if a number like 6 is in prophrases in row #1 or not!
what can i do for that?
something like this but in correct form!
SELECT * FROM TABLENAME WHERE 6 IN prophrases
you should just use FIND_IN_SET()
SELECT rowid
FROM TABLENAME
WHERE FIND_IN_SET('6', prophrases)
This will work if you are only dealing with single digits
select * from tablename where prophrases like '%6%'
This will work if you are going to have number with more than one digit and there are no spaces between commas.
select * from tablename where prophrases like '%,6,%' or prophrases like '6,%' or prophrases like '%,6'
You need to use a like statement
SELECT * FROM TABLENAME WHERE prophrases LIKE '%6%'
However if you have multiple digit numbers that could cause some unexpected results, so you might have to amend it like
SELECT * FROM TABLENAME
WHERE prophrases LIKE '%,6,%'
OR prophrases LIKE '6,%'
OR prophrases LIKE '%,6'
The first part matches 6 in between commas, the second one matches a 6 at the beginning followed by a comma, the third matches a comma followed by a six a the end.
Storing data like that is not the best way of doing it. You are probably better off having another table that has a one-to-many relationship.

A query to check the first 2 chars of a string

I would like to know if it's possible to make a query to check the first two chars of a string?
For example, I have these entries in my table:
toto
topo
poto
I want to get the entries when the two first chars are to.
As simple as this:
SELECT *
FROM TABLE
WHERE `name` LIKE "to%"
to% means that the word should start by to and can be followed by other character
Try this:
SELECT * FROM myTable WHERE RowName LIKE 'to%'
select fieldName
from tableName
where fieldName like 'to%'
if you need to check the first 2 chars of a string but return all results..
SELECT fieldname,
if(left(fieldname,2)='XX',1,0) as XCheck
FROM tablename

Search MySQL table for a kind of results

How can I search within a MySQL table for results ending in anything except ".jpg"?
Thanks.
You don't need to involve regular expressions, you can just do:
SELECT my_fields
FROM my_table
WHERE my_field NOT LIKE '%.jpg'
SELECT *
FROM mytable
WHERE myfield NOT RLIKE '\\.jpg$'