How to select CamelCase words in Mysql RegexP - mysql

In 'plain' Regex this works for me:
\w+[A-Z][a-z]\w+
as per this example: https://regex101.com/r/VqsTXL/1
However doing the equivalent in Regexp fails to find CamelCase:
[[:<:]][A-Z][a-z][[:>:]]
as per this example: http://sqlfiddle.com/#!9/ad35d8/1

In MySQL version before 8, you can "emulate" \w using [[:alnum:]_]:
Select * from PatternTester where Binary Name regexp '[[:alnum:]_]+[A-Z][a-z][[:alnum:]_]+'
See the regex demo.
SQL:
Create Table PatternTester
(Name varchar(100));
Insert into PatternTester (Name)
values ('CamelCase'),
('Camelcase'),
('CAMELCASE'),
('CamelCase')
Select * from PatternTester where Binary Name regexp '[[:alnum:]_]+[A-Z][a-z][[:alnum:]_]+'
See SQL demo.

Related

Add comma between every character of a string on mysql

Is it possible to add a comma between each character of a string on mysql.
From my_table
id
name
1
hello
To :
id
name
1
h,e,l,l,o
Another MySQL8 REGEXP_REPLACE -
SELECT id, REGEXP_REPLACE(name, '(?=.)(?<=.)', ',')
FROM my_table;
db<>fiddle and regular expressions 101
Since MySQL 8.0.4 the regex implementation has been using International Components for Unicode (ICU) - patterns and behavior are based on Perl’s regular expressions
If you are using MySQL 8.0, you could try REGEXP_REPLACE function.
SELECT REGEXP_REPLACE(name, "(.)(?!$)", "$1,")
FROM my_table
See demo here

Query the user name of the double letter

I want to find usernames with reduplicated words in my database
Below is my SQL statement:
SELECT * FROM users WHERE BINARY `name` regexp 'a{2}|b{2}|c{2}|d{2}|e{2}|f{2}|g{2}|h{2}|i{2}|j{2}|k{2}|l{2}|m{2}|n{2}|o{2}|p{2}|q{2}|r{2}|s{2}|t{2}|u{2}|v{2}|w{2}|x{2}|y{2}|z{2}'
I guess there should be an easier way, but I don't know how to do it.
I also want to find usernames like 'ABAA' or 'AABA', but I won't write this SQL or say this regex
As MySQL's regex engine does not support backreferences, your approach is reasonable. To simplify your query, you could maintain a table of double letters:
table: letters (val)
aa
bb
...
zz
Now your query can be simplified to:
SELECT *
FROM users u
WHERE EXISTS (
SELECT 1
FROM letters l
WHERS INSTR(u.name, l.val) > 0
);
Try:
create table users(name varchar(100));
insert into users (name) values
("abaa"),
("bba"),
("abc"),
("aCCa");
SELECT * FROM users WHERE LOWER(name) REGEXP 'aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu|vv|ww|xx|yy|zz';
Prints:
abaa
bba
aCCa
i think i found the answer,I upgraded my MySQL database to MariaDB.
this is my sql statement:
SELECT * FROM users WHERE `name` REGEXP '([a-z])\\1'
This is the sql statement to query like 'aaba':
SELECT * FROM users WHERE `name` REGEXP '([a-z])\\1((?!\\1)[a-z])\\1'
Note: Because MariaDB uses the C escape syntax in strings (for example, "\n" to represent the newline character), you must double any "" that you use in your REGEXP strings.
In MySQL 8.0
WHERE name REGEXP '([a-z])\\1'
Let the collation take care of ignore case.

Regex bring just the match MySQL

I am trying to getting just the first two words on sql query, I am using the match: ^\w{2}- but with no success because nothing is coming to me, I need to get those values
BA, CE, DF, ES, GO, I don't know how can I do that, below some data example.
SC&Tipo=FM
SC&Tipo=Web
SC&Tipo=Comunitaria
RS&Tipo=Todas
RS&Tipo=AM
RS&Tipo=FM
RS&Tipo=Web
RS&Tipo=Comunitaria
BA-Salvador&Tipo=12horas
CE-Fortaleza&Tipo=12horas
CE-Interior&Tipo=12horas
DF-Brasilia&Tipo=12horas
ES-Interior&Tipo=12horas
ES-Vitoria&Tipo=12horas
GO-Goiania&Tipo=12horas
MG-ZonaDaMata/LestedeMinas&Tipo=12horas
MG-AltoParanaiba&Tipo=12horas
MG-BeloHorizonte&Tipo=12horas
MG-CentroOestedeMinas&Tipo=12horas
Query: SELECT * FROM tabel WHERE filter REGEXP '^\w{2}-'
EDIT SOLVED:
To solve the query should be:
SELECT SUBSTRING(column, 1, 2) AS column FROM table WHERE column REGEXP '^[[:alnum:]_]{2}-'
MySQL doesn't support the character class \w or \d. Instead of \w you have to use [[:alnum:]]. You can find all the supported character classes on the official MySQL documentation.
So you can use the following solution using REGEXP:
SELECT *
FROM table_name
WHERE filter REGEXP '^[[:alnum:]]{2}-'
You can use the following to get the result with regular expression too, using REGEXP_SUBSTR:
SELECT REGEXP_SUBSTR(filter, '^[[:alnum:]]{2}-')
FROM table_name
WHERE filter REGEXP '^[[:alnum:]]{2}-';
Or another solution using HAVING to filter the result:
SELECT REGEXP_SUBSTR(filter, '^[[:alnum:]]{2}-') AS colResult
FROM table_name
HAVING colResult IS NOT NULL;
To get the value before MySQL 8.0 you can use the following with LEFT:
SELECT LEFT(filter, 3)
FROM table_name
WHERE filter REGEXP '^[[:alnum:]]{2}-';
demo: https://www.db-fiddle.com/f/7mJEmCkEiYhCYK3PcEZTNE/0
Using SUBSTRING(<column>, 1, 2) should also work..
More or less like below
SELECT
<column>
, SUBSTRING(<column>, 1, 2)
FROM
<table>
WHERE
SUBSTRING(<column>, 1, 2) IN ('BA' [,<value>..])
Some things are BNF (Backus-Naur form) in the SQL code.
<..> means replace with what you need.
[, ..] means optional unlimited repeat the comma in there is part off SQL syntax

Oracle SQL - Regex to search columns with only 1 letter

I want to to create a regex to find all columns that only have a single character ([A-Z]) as name, like N or M but not NM.
I've tried:
SELECT * FROM 'table' WHERE Name REGEXP '^[A-Z]'
But it's not displaying the expected result.
Try ^[A-Z]$.
You then state that this character is first and also last character of the value.
The regex functions in Oracle work only on one column. So, to search for just one character in a column, you would do the following:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$');
Now, to search all the char/varchar columns on your table, you'll need to chain the regex expressions together, like so:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$') or REGEXP_LIKE (col3, '^[A-z]$');
SQL solution:
where name in ('N','M')

like clause doesn't work as it said in MySQL SQL

I have a table, such as
create table table1(
name varchar(32),
);
And there's some data in it. When I select like this:
select * from table1 where name like 'Jack2%';
there will be Jack2.
But if I select like this:
select * from table1 where name like 'Jack[0-9]%';
there will be nothing;
And I also tried regexp to subsitute like, but it also didn't work!
What's wrong?
You've confused two different pattern-matching mechanisms. SQL LIKE uses % to match anything and _ to match any single character; it does not have anything like [0-9] to match a digit. That looks like a character class from a regular expression.
Standard SQL has no support for regular expressions at all, but MySQL does - you just have to use RLIKE (or REGEXP, but that doesn't read as nicely IMO) instead of LIKE. But that means that you have to replace the % with the regular-expression equivalent .*, too.
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
Fiddle
MySQL REGEX
select * from Table1 where `name` REGEXP 'Jack[0-9]'
You can use RLIKE instead
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
And please note the the '%' operator won't work with RLIKE, you have to use a regular expression pattern like '.*' instead.