Find occurrence of a substring in string in MySQL? - 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';

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>

Regex for specific pattern - String followed by numbers

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.

Advanced MySQL pattern matching (beyond LIKE...%)

This is the example of my current MySQL my_table...
id name code
1 111 XXXX123456XXXXXXXXXXXXXX
2 222 XXXX133456XXXXXXX5XXXXXX
3 333 XXXX123454XXX11XXXXXXABC
Code is a 24 character hexadecimal value where X is the wildcard.
I need to write a query that will return the NAME based on the CODE without a wildcard value X.
The given CODE value will be exact but it should compare the string in place, X could match any character.
For example:
SELECT name FROM my_table where code = '012312345611111111111111';
name
111
SELECT name FROM my_table where code = '000013345622222225123456';
name
222
SELECT name FROM my_table where code = '000123454ABC11234567FABC';
name
333
You can use like for this. Are you aware of the _ wildcard?
select t.*
from t
where #YourCode like replace(t.code, 'X', '_');
Of course, you can use regular expressions too. The regular expression would be: concat('^', replace(t.code, 'X', '.'), '$').

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 regular expressions

i'm trying to to look for all strings that start with the letter D. in my mysql query i have this.
REGEXP '^[D]{4}$'
Problem is it returns everything what stars with D and End with D too.
example:
DDD 123 - true
D 123 - true
DDDD 1234 - true
SSS 123 D - returns true, but should be false.
Any idea what i'm missing?
"...for all strings that start with the letter D"
You can simply do it by using LIKE
WHERE columnName LIKE 'D%'
or this pattern in regex.
WHERE columnName REGEXP '^D.*'
SQLFiddle Demo (for both queries)
select * from tablename where columnname RLIKE '^D'
or
select * from tablename where columnname REGEXP '^D'