Regex for specific pattern - String followed by numbers - mysql

We're trying to use a REGEX expression inside MySQL.
Say we have a 2-column table with 5 rows as follow:
1 marketing
2 marketing1
3 marketing12
4 office5
5 marketing44Tomorrow
I'd like to have a SELECT statement that returns: marketing, marketing1, marketing12. Meaning a string (marketing) followed by nothing or by a number only.
This statement:
select * from ddd
where column_name2 REGEXP 'marketing[0-9]'
doesn't work as it does not return "marketing" alone and it will return "marketing44Tomorrow".

You can use : marketing([0-9]+)?[[:>:]]
`marketing` - any word start with **marketing**
`([0-9]+)` - any digit where....
1. `?` - Maybe there may there not
2. `[[:>:]]` - Must be the last
Result:
SELECT * FROM ddd WHERE column_name2 REGEXP 'marketing([0-9]+)?[[:>:]]'

try this,
select * from ddd where column_name2 REGEXP 'marketing[0-9]$'

As a conclusion, the perfect answer to my question in the MySQL context is:
SELECT * FROM ddd WHERE column_name2 REGEXP 'marketing([0-9]+)?[[:>:]]'
"MJN Belief" got it almost right up here.

Related

Return strings which contains number on specific position

I am basic on SQL queries and I need some help.
I have to select all string values which contains number e.g. 7 only on specific position in that string.
For example:
I have string: 987654321 and if on position 3 I will have number 7, then it should be selected.
So in example this string will be selected, because on 3rd position I have number 7.
Is there any SQL function for that, or something which could help me?
EDIT:
Example table
TABLE
Numbers Value
987654321 1
123456789 2
789009871 3
654321092 4
847949372 5
Output:
TABLE
Numbers Value
987654321 1
847949372 5
Statement:
SELECT table.numbers
FROM TABLE
WHERE substr(table.numbers,3,1)='7' <--- what to do here? --->
Many thanks in advance.
For a regex option, you may use MySQL's REGEXP operator:
SELECT *
FROM yourTable
WHERE num REGEXP '^[0-9]{2}7';
On Oracle, you could use REGEXP_LIKE:
SELECT *
FROM yourTable
WHERE REGEXP_LIKE(num, '^[0-9]{2}7');
You should use case statement.
select case when substr(stringcol, 3,1) = '7' then stringcol else "not valid" end as stringcol from <Table Name>

MYSQL - Find rows, where part of search string matches part of value in column

I wasn't able to find this anywhere, here's my problem:
I have a string like '1 2 3 4 5' and then I have a mysql table that has a column, let's call it numbers, that look like this:
numbers
1 2 6 8 9 14
3
1 5 3 6 9
7 8 9 23 44
10
I am trying to find the easiest way (hopefully in a single query) to find the rows, where any of the numbers in my search string (1 or 2 or 3 or 4 or 5) is contained in the numbers column. In the give example I am looking for rows with 1,2 and 3 (since they share numbers with my search string).
I am trying to do this with a single query and no loops.
Thanks!
The best solution would be to get rid of the column containing a list of values, and use a schema where each value is in its own row. Then you can use WHERE number IN (1, 2, 3, 4, 5) and join this with the table containing the rest of the data.
But if you can't change the schema, you can use a regular expression.
SELECT *
FROM yourTable
WHERE numbers REGEXP '[[:<:]](1|2|3|4|5)[[:<:]]'
[[:<:]] and [[:<:]] match the beginning and end of words.
Note that this type of search will be very slow if the table is large, because it's not feasible to index it.
Here is a start point (split string function) : http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/ := SplitString(string,delimiter,position)
Create a function so it converts a string to an array := stringSplitted(string,delimiter)
Create a function so it compares two arrays :=arrayIntersect(array1, array2)
SELECT numbers
FROM table
WHERE arrayIntersect(#argument, numbers)
Two function definitions with loops and one single query without any loop
SELECT * FROM MyTable WHERE (numbers LIKE '%1%' OR numbers LIKE '%2%')
or you can also use REGEX something like this
SELECT * FROM events WHERE id REGEXP '5587$'

MySQL: Select values containing only one digit

Let's say I have a column with emails:
test1#test.com
test2#test.com
test3test#tes.com
test123#test.com
test321#test.com
test23test.com
How can I select only those that contains only one digit?
Result should be:
test1#test.com
test2#test.com
test3test#tes.com
I tried:
REGEXP '[[:digit:]]{1}' and REGEXP '[0-9]{1}' but it shows all results that contain AT LEAST one digit
try to use ^[^0-9]*[0-9]{1}[^0-9]*$
If you don't specify number of repetitions, it is one copy by default.
Try something like
REGEXP '(^[0-9]+)([0-9])#.+'

mysql replace string + next one char

Is it possible to REPLACE a string + next character in MySQL? Something like LIKE underscore.
For example, if text column is this:
12 13 14 14_B 15 14_A, REPLACE all 14_* with an empty character, and replaced text should be:
12 13 14 15
You'll be looking to do this using a regular expression UDF in MySQL. Key ingredients are
regular expression UDF - check here
The regular expression itself
If you will ONLY ever see 2 to 4 of these that you need replaced, a poor man's working approach (SQL Fiddle):
SELECT *,IF(LOCATE('14_',B)+3<=Length(B),
INSERT(B,LOCATE('14_',B),4,''),B) C
FROM
(
SELECT *,IF(LOCATE('14_',A)+3<=Length(A),
INSERT(A,LOCATE('14_',A),4,''),A) B
FROM (
SELECT *,IF(LOCATE('14_',x)+3<=Length(X),
INSERT(X,LOCATE('14_',x),4,''),X) A
FROM X
) Q1
) Q2
I've only catered for 3 replacements but you can easily expand the pattern. Include only the columns from the base table needed in the outermost query.

Find occurrence of a substring in string in MySQL?

I'm using this query to getresults from my database:
MATCH(`Text2`) AGAINST ('$s')
I want to get only results when there is a full match of the string, like when on google when you search between quotes "".
How can I do this with Match/MySQL?
EG: Query is "ab cd"
ID | Text
1 ab cd
2 aab cda
3 aab a cd
Row 1 and 2 should be returned
SELECT FROM your_table WHERE Text2 LIKE '%yourstring%';
Try this::
If you need the wild search irrespective to the cases ::
SELECT FROM LOWER(mytable) WHERE LOWER(Text2) LIKE LOWER('%yourstring%');
You can use REGEXP for this purpose too.
SELECT *
FROM `tableName`
WHERE `columnName` REGEXP 'ab cd';