Looking to extract data between parentheses in a string via MYSQL - mysql

Can someone please help. I have been searching and encountered/modified this code I am getting a 1 or 0 as a result. 1 if there is something between () and 0 if there is not. I am looking to find exactly what is between them not if there is something. So if I have a string in afield that looks like this: "ABC (989) Hello" currently I get 1 as my result I would like to get "989". Any help would be greatly appreciated.
select , OUTCNTCTNOTE regexp '[(]|\\[)]' as test
from trcalls.callcoding;

To complete the first answer, because the third parameter passed to substr is the length of the substring, we need to subtract the index of the opening parantheses, so:
substr(columnname,instr(columnname,"(") + 1, instr(columnname,")") - instr(columnname,"(") - 1)
should do the trick

select substr(columnname,instr(columnname,"(") + 1, instr(columnname,")")) as temp from mytable
something close, I tested this. Please see if this helps!

Mysql's regexes don't support capturing or replacing. They're purely for matching. You'd need to use regular string operations to do the actual extraction:
SELECT ...string stuff here...
FROM yourtable
WHERE OUTCNTCTNOTE regexp ....
If your strings are fairly 'regular' and you don't have to worry about multiple sets of brackets in any field, then using LOCATE() and SUBSTR() would do the trick.

Related

SQL replace all specified keys

I have one column(varchar) containing only json string within one table. I want replace all keys with "" on that column. How can I do that using sql? My database is MySQL.
For example:
|--------------------------------------------------------------------|
| t_column |
|--------------------------------------------------------------------|
| {"name":"mike","email":"xxx#example.com","isManage":false,"age":22}|
|--------------------------------------------------------------------|
SELECT replace(t_column, regexp, "") FROM t_table
I expect:
mikexxx#example.comfalse22
How to write that regexp?
Start from
select t_column->'$.*' from test
This will return a JSON array of attribute values:
[22, "mike", "xxx#example.com", false]
This might be already all you need, and you can try something like
select *
from test
where t_column->'$.*' like '%mike%';
Unfortunately there seems to be no native way to join array values to a single string like JSON_ARRAY_CONCAT(). In MySQL 8.0 you can try REGEXP_REPLACE() and strip all JSON characters:
select regexp_replace(t_column->'$.*', '[" ,\\[\\]]', '') from test
which will return '22mikexxx#example.comfalse'.
If the values can contain one of those characters, they will also be removed.
Note: That isn't very reliable. But it's all I can do in a "simple" way.
See demo on db-fiddle.
I could be making it too simplistic, but this is just a mockup based on your comment. I can formalize it into a query if it fits your requirement.
Let's say you get your JSON string to this format where you replace all the double quotes and curly brackets and then add a comma at the end. After playing with replace and concat_ws, you are now left with:
name:mike,email:xxx#example.com,isManage:false,age:22,
With this format, every value is now preceded by a semicolon and followed by a comma, which is not true for the key. Let's say you now want to see if this JSON string has the value "mike" in it. This, you could achieve using
select * from your_table where json_col like '%:mike,%';
If you really want to solve the problem with your approach then the question becomes
What is the regex that selects all the undesired text from the string {"name":"mike","email":"xxx#example.com","isManage":false,"age":22} ?
Then the answer would be: {\"name\":\"|\"email\":\"|\",\"isManage\":|,\"age\":|}
But as others let you notice I would actually approach the problem parsing JSONs. Look up for functions json_value and json_query
Hope I helped
PS: Keep close attention on how I structured the bolded sentence. Any difference changes the problem.
EDIT:
If you want a more generic expression, something like select all the text that is not a value on a json-formatted string, you can use this one:
{|",|"\w+\":|"|,|}

SQL query to select strings that contain a "Unit Separator" character

I have table like this
I want get those record which content Unit Separator
I have try many things but not getting result.I try with char(31) and 0x1f and many other ways but not getting desired result.This is my query which i try
SELECT * FROM `submissions_answers` WHERE `question_id`=90 AND `answer` like '%0x1f%'
How can i do this? Please help me..
Problem
The expression you tried won't work because answer LIKE '%0x1f%' is looking for a string with literally '0x1f' as part of it - it doesn't get converted to an ASCII code.
Solutions
Some alternatives to this part of the expression that ought to work are:-
answer LIKE CONCAT('%', 0x1F, '%')
answer REGEXP 0x1F
INSTR(answer, 0x1F) > 0
Further consideration
If none of these work then there may be a further possibility. Are you sure the character seen in the strings is actually 0x1F? I only ask because the first thing I tried was to paste in ␟ but it turns out MySQL see this as a decimal character code of 226 rather than 31. Not sure which client you are using but if the 0x1F character is in the string, it might not actually appear in the output.
Demo
Some tests demonstrating the points above: SQL Fiddle demo
You can use:
SELECT * FROM submissions_answers WHERE question_id=90 AND instr(answer,char(31))>0
The keyword here being the INSTR MySQL function, which you can read about here. This function returns the position of the first occurrence of substring (char(31)) in the string (answer).
Yet another way...
SELECT * FROM `submissions_answers`
WHERE `question_id`=90
AND HEX(`answer`) REGEXP '^(..)*1F'
Explanation of the regexp:
^ - start matching at the beginning (of answer)
(..)* -- match any number (*) of 2-byte things (..)
then match 1F, the hex for US.
You could convert the answer column into a HEX value, and then look for values containing that hex string.
SELECT * FROM `submissions_answers`
WHERE HEX(`answer`) LIKE '%E2909F%'

How to make this regex in mysql?

A few min ago I found out that mysql accepts regex, and is great becouse I think it can solve my problem, but I don't know how to write it. So, I need something like this:
SELECT name FROM products WHERE name REGEXP 'regex code'
To give a little more explanation, the name must be in this format: 123425HT and not string99-123425HT. The 123425HT and string99-123425HT is taken arbitrary
Please Help, thanks!
Something like that:
SELECT * FROM t2 WHERE str REGEXP "^([0-9]+)([a-zA-Z]{2})$";
This regexp will found strings which starting from any digits and two small or big characters, for example:
123123hd
12345435MF
6572Sg
If you want use only 6 digits change from [0-9]+ to [0-9]{6}

Mysql searching for string in text

I can't help but think my syntax is wrong.
SELECT * FROM `rework` WHERE rw_pd LIKE 'FIB'
The SQL executes with a result of 0 rows. I KNOW there are rows with that string in that field. Anybody see any stupid mistakes?
You have to specify the wildcard if your have strings which contains FIB chained to other text:
SELECT * FROM `rework` WHERE rw_pd LIKE '%FIB%'
Check also this link for various wildcards usage.
Have you tried adding %, which is a wildcard.
SELECT * FROM rework WHERE rw_pd LIKE '%FIB%'

MySql pattern matching

I need a MySQL pattern to match a number, followed by a question mark.
I need something like
... like '%[0-9]?%'
but I have no idea how to create this regular expression.
http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html does not help.
Thanks!
you could try this:
SELECT * FROM YourTable WHERE YourField REGEXP '[0-9]\\?'
That will return rows where YourField contains a number followed by a ? anywhere in the value.
If you want it to only match if the whole field is a number followed by a ?. I.e. 9? then you could use this regex instead:
^[0-9]\\?$
I guess you're looking for something like this:
select * from table
where field rlike '[0-9]\\?'
Remember to escape the question mark. Otherwise, it will make the number optional.
Source.