MySQL query to delete all the random data between two known strings - mysql

I have a MySQl Database, the table name is "post_data" field name is "content_data"
I want to delete all the random data between two known strings.
i want to delete all text between 'www.OldDomain.com/' & '/www.NewDomian.com'
For example:
www.OldDomain.com/redirect?RandomTextUrl/www.NewDomian.com/txturl
any suggestion will be appreciated?

Assuming that each of these strings appears exactly once in content_data and in the correct order:
select concat(substring_index(content_data, 'www.OldDomain.com', 1),
'www.OldDomain.com',
'www.NewDomian.com',
substring_index(content_data, 'www.NewDomian.com', -1)
)

If you are looking to struggle and do it using MySQL You could use SUBSTRING_INDEX
I would suggest to use PHP/Java/C# or any other language to do these kinds of string parsing though.

You can use LOCATE and SUBSTRING functions to get the string between two desired strings.
SELECT SUBSTRING(
data,
LOCATE('www.OldDomain.com/', data) + LENGTH('www.OldDomain.com/'),
LOCATE('/www.NewDomain.com', data) - (LOCATE('www.OldDomain.com/', data) + LENGTH('www.OldDomain.com/'))
) as output
FROM TABLE_NAME;
NOTE: TABLE_NAME is your table name, data is your column name and output is the alias for the extracted data.
Sample Run
CREATE TABLE stack (data varchar(100));
INSERT INTO stack VALUES('www.OldDomain.com/redirect?RandomTextUrl/www.NewDomain.com/txturl');
INSERT INTO stack VALUES('BEFOREwww.OldDomain.com/redirect?RandomTextUrl/www.NewDomain.com/txturl');
INSERT INTO stack VALUES('www.OldDomain.com/redirect?RandomTextUrl/www.NewDomain.com/txturlAFTER');

Related

modify the data of column while copying the data of column from other table

In mysql, I am trying to copy the data from one column to other column of different table. I have used the below command to achieve this.
insert ignore into user(payload) select payload from group;
data in old column is data:test
data in new column should be {"payload":"data:test"}
As of now when I used the above insert command, it just copies the data as is from old column, I want the new column to be updated as {"payload":"data:test"}
Actually that quite simple with the use of the CONCAT() function
insert ignore into user(payload)
select CONCAT( '{"payload":"', payload, '"}' ) from group;
A tutorial to explain the CONCAT() function

Match Numeric value after comma separated, concatenated by underscore values using MYSQL/MariaDB & REGEXP_SUBSTR

I have field column values stored like:
texta_123,textb_456
My SQL:
SELECT *
FROM mytable
WHERE 456 = REGEXP_SUBSTR(mytable.concatenated_csv_values, 'textb_(?<number>[0-9]+)')
NOTE: I'm aware there are multiple ways of doing this, but for the purposes of example I simplified my query substantially; the part I need to work is REGEXP_SUBSTR()
Effectively, I want to: "query results where an id equals the numeric value extracted after an underscore in a column with comma-separated values"
When I test my Regex, it seems to work fine.
However, in MySQL (technically, I'm using MariaDB 10.4.19), when I run the query I get a warning: "Warning: #1292 Truncated incorrect INTEGER value:textb_456"
You should seriously consider fixing your database design to not store unnormalized CSV data like this. As a temporary workaround, we can use REGEXP_REPLACE along with FIND_IN_SET:
SELECT *
FROM mytable
WHERE FIND_IN_SET(
'456',
REGEXP_REPLACE(concatenated_csv_values, '^.*_', '')) > 0;
The regex trick used here would convert a CSV input of texta_123,textb_456 to just 123,456. Then, we can easily search for a given ID using FIND_IN_SET.

How to change the field name in big query?

I have a nested JSON to upload in Big Query.
{
"status":{
"sleep":"12333",
"wake":"3837"
}
}
After inserting it in Big Query, I am getting the field names as :
status_sleep and status_wake
I require the field names to be seperated by delimeters like '.' or any other delimeter
status.sleep and status.wake
Please suggest how to add the field deimeter. I checked there is a field delimeter key for uploading the data in csv format.
After you insert data with above schema you have record named status with two fields in it status.sleep and status.wake
When you query as
SELECT * FROM yourtable
without providing aliases - you will get output named as status_sleep and status_wake because dot notation is reserved for referencing nested data.
But you still can reference your data with dots as in below
SELECT status.sleep as sleep, status.wake as wake FROM yourtable

MySQL substring between two strings

I need a hand to solve a problem with my column field.
I need to extract the string in between these two different "patterns" of strings for example:
[...string] contract= 1234567890123350566076070666 issued= [string
...]
I want to extract the string in between 'contract=' and 'issued='
At the present moment I'm using
SELECT substring(substring_index(licence_key,'contract=',-1),1,40) FROM table
The problem is that this string in between doesn't have always 40 characters so it's not fixed length and so the data that comes before and after that. It's a volatile data.
Do you known how I can handle that?
Just use substring_index() twice:
SELECT substring_index(substring_index(licence_key, 'contract=', -1),
'issued=', 1)
FROM table;
If this string does not match then give the total result.
If you want to replace then you can use like this.
UPDATE questions set question= REPLACE(question, '<xml></xml>', '') WHERE question like '%<xml>%';
UPDATE questions set question= REPLACE(question, substring_index(substring_index(question, '<xml>', -1), '</xml>', 1), '') WHERE question like '%<xml>%';

Getting a substring in a mysql query

In my mysql table, one of the fields holds data of the nature:
{"gateway":"somevalue","location":"http://www.somesite.org/en/someresource","ip":"100.0.0.9"}
I need to extract the value of the location attribute alone from this field, which is
http://www.somesite.org/en/someresource
in the this case. How do I write a query to achieve this?
Apart from the fact that you better off not storing delimited values of any form (including JSON) in the database, but rather normalize your data, you can leverage very handy SUBSTRING_INDEX() function in the following way
SELECT TRIM(BOTH '"' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '"location":', -1), ",", 1)) location
FROM table_name
WHERE ...