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
Related
I tried to use "||" for concatenation:
The query I used:
"SELECT id, name, age, age||id FROM myTable;"
This is the output:
Can anyone tell me why the output is not 201 (in first column) and 162(in second column)?? Also it gives similar outputs when I use two attributes that are of varchar datatype and the above two attributes are of int datatype.
Can anyone tell me why the output is not 201
its because, in mysql, you need to enable PIPES_AS_CONCAT. in order to work with ||
If the PIPES_AS_CONCAT SQL mode is enabled, || signifies the
SQL-standard string concatenation operator (like CONCAT()).
You can set it using phpmyadmin->variables->searchFor SQL_MODE
Refer mysql doc
But i would suggest you to use
CONCAT(columnName1, columnName2, ...)
There is no such concatenation in mySQL. This is Oracle SQL dialect. You have to use the CONCAT function in mysql
SELECT id, name, age, CONCAT(age,id) FROM myTable
visit the Mysql Documentations :
on this link you will find a list of String Functions and Operators
but you not use|| to concatenate caratcters or strings but do this :
SELECT id, name, age, concat(age,id) FROM myTable; or
SELECT id, name, age, concat_ws(' ',age,id) FROM myTable; if you want to space the age with id like this for example 23 1. 23 for age and 1 for id.
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.
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
EDITED:
i have a mysql table row with 'option_name'.
The option names are:
my_option_en
my_option_de
my_option_adv_en
my_option_adv_de
my_option_sc_en
my_option_sc_de
my_option_labs_en
my_option_labs_de
A query for the option name my_option_ should return my_option_en and my_option_de but not any other option names
What i need is look for given string ending with 2 any letters (language iso
code) and no any character after the 2 language iso letters.
How can i realize it with Mysql REGEXP? Thanks
In MySQL regexp form, you would need
SELECT *
FROM yourtable
WHERE option_name REGEXP '^my_option_[a-z]{2}$'
This will discard every row where option_name starts with my_option_adv:
select option_name
from yourtable
where not(option_name regexp '^my_option_adv')
Demo here: db-fiddle.com
WHERE CHAR_LENGTH(option_name) = 12
will do exactly what you stated. But I suspect that is not good enough.
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')