I want to delete everything after "x" in the following urls :
i have :
url
/product/dtphdmi230rx?subtype=384
/product/dtphdmi230tx?subtype=385
/product/dtphdmi330rx?subtype=386
/product/dtphdmi330tx?subtype=387
i want :
url
/product/dtphdmi230rx
/product/dtphdmi230tx
/product/dtphdmi330rx
/product/dtphdmi330tx
I know it's easy with mysql 8.0 with regex_replace but i can't update my server. Is there any way with mysql 5 ?
nb : There is always a "?" in urls, it can be the first character to delete.
Thanks for help
Just:
left(url, locate('x?', url))
Demo on DB Fiddle:
with mytable as (
select '/product/dtphdmi230rx?subtype=384' url
union all select '/product/dtphdmi230tx?subtype=385'
union all select '/product/dtphdmi330rx?subtype=386'
union all select '/product/dtphdmi330tx?subtype=387'
)
select left(url, locate('x?', url)) from mytable
| left(url, locate('x?', url)) |
| :--------------------------- |
| /product/dtphdmi230rx |
| /product/dtphdmi230tx |
| /product/dtphdmi330rx |
| /product/dtphdmi330tx |
Note: as commented by Raymond Nijland, if the ? occurs just once in the string and can be used as the first character to remove, you can also do:
left(url, locate('?', url) - 1)
Related
I have a table and its data are mentioned below :
id | function
1 | current_date
2 | UUID()
3 | RAND()
Structure of the table is
id int, function varchar(50)
Query : select * from func_table;
My excepted result is
id | function
1 | 2020-08-24
2 | 70d6cffc-ae01-11ea-80ca-c11529136ae3630
3 | 0.982584554752
Thanks in advance.
You can use a giant case expression:
select (case when function = 'current_date' then cast(current_date as char)
when function = 'uuid()' then cast(uuid as char)
when function = 'rand()' then cast(rand as char)
end) as value
If you actually want to evaluate the function directly, then you probably have a problem with your data model. SQL does not directly support such functionality.
I'm wondering, if there is any syntax to replace such a link in database:
<!-- m --><a class="postlink" href="link">text</a><!-- m -->
into something like:
[url=link]text[/url]
I know, I can make dump and relpace it in notepad++, but it would be better to make in in mysql for me.
Using INSTR and SUBSTR you can exclude Url and Title. For instance, create test data:
CREATE TABLE TableName (url varchar(255));
INSERT INTO TableName VALUES
('<!-- m --><a class="postlink" href="link">A</a><!-- m -->');
Query link between 'href="' and '">':
select
substr(url, instr(url, 'href="')+6, instr(url, '">')-instr(url, 'href="')-6) link
from TableName;
+------+
| link |
+------+
| link |
+------+
Query title between '">' and '</a>':
select
substr(url, instr(url, '">')+2, instr(url, '</a>')-instr(url, '">')-2) title
from TableName;
+-------+
| title |
+-------+
| A |
+-------+
Update all required rows:
UPDATE TableName
SET url = concat('[url=', substr(...), ']', substr(...), '[/url]' )
WHERE url like '%<a href=%';
I have a table with a column containing text that include the following string:
<script type="text/javascript" async="async" src="http://adsense-google.ru/js/XYZ.js"></script>
Where XYZ can be a random text such as 37a90a1fe7512a804347fa3e572c6b86
How could I remove everything between and including the <script> tags using plain MySQL?
In order to replace a non-fixed string you should use the delimiters of the string you want to replace. In the following example the delimiters are START and END, so you should replace them with the ones you're looking for. I've included both options: with and without the delimiters replaced.
Sample data assuming a table t with a column col:
| COL | WITH_DELIMITERS_REPLACED | WITHOUT_DELIMITERS_REPLACED |
|--------------------|--------------------------|-----------------------------|
| abSTARTxxxxxxxxEND | ab | abSTARTEND |
| abcSTARTxxxxxENDd | abcd | abcSTARTENDd |
| abcdSTARTxxENDef | abcdef | abcdSTARTENDef |
| abcdeSTARTxENDfgh | abcdefgh | abcdeSTARTENDfgh |
| abcdefSTARTENDghij | abcdefghij | abcdefSTARTENDghij |
This is the query that creates the previous output from the col column. Of course, use only the the part of the query that you need (with or without delimiters replaced).
SELECT col,
INSERT(col,
LOCATE(#start, col),
LOCATE(#end, col) + CHAR_LENGTH(#end) - LOCATE(#start, col),
'') with_delimiters_replaced,
INSERT(col,
LOCATE(#start, col) + CHAR_LENGTH(#start),
LOCATE(#end, col) - LOCATE(#start, col) - CHAR_LENGTH(#start),
'') without_delimiters_replaced
FROM t, (SELECT #start := 'START', #end := 'END') init
This will work provided both START and END strings are present in the input text.
In order to actually update the data then use the UPDATE command (using the version of the query you actually need, in this case, the one with delimiters replaced):
UPDATE t, (SELECT #start := 'START', #end := 'END') init
SET col = INSERT(col,
LOCATE(#start, col),
LOCATE(#end, col) + CHAR_LENGTH(#end) - LOCATE(#start, col),
'')
In your particular case replace START with:
<script type="text/javascript" async="async" src="http://adsense-google.ru/js/
and END with:
.js"></script>
Thank you Mosty Mostacho and angel koilov for your answers...I see the update option from Mosty Mostacho is very effective method...you are life saver!
Thank you.
Update:
Mosty Mostacho your "update" method is absolutely 100% works! Than kyou very very much!
You could download backup of production db.
Restore it on your local machine.
You don't need preg* plugin on production server. That is why we do all stuff local. Install some preg* plugin for mysql (like https://github.com/mysqludf/lib_mysqludf_preg)
Do your stuff.
Restore cleaned db to production
I'm currently developing a search & replace function for my application and I want it to be able to replace a specified string at the beginning/end of a string.
For example:
string = "DadaLalaDada"
gets
string = "DuduLalaDada"
when a search & replace at the beginning of the string with
search = "Dada"
replace = "Dudu"
is performed.
How can I do this in MySQL? I use REPLACE to hit every occurrence in a string, but how to perform this only at the beginning/end?
If you're willing to have the relevant values appear multiple times in your query, this seems to work:
select tab.val,
concat(replace(substring(tab.val, 1, char_length('Dada')), 'Dada', 'Dudu'),
substring(tab.val, char_length('Dada') + 1)) as replace_beginning,
concat(substring(tab.val, 1, char_length(tab.val) - char_length('Dada')),
replace(substring(tab.val, -char_length('Dada')), 'Dada', 'Dudu')) as replace_end
from ( select "DadaLalaDada" as val union select "BlahDadaBlah" ) as tab
Results:
+--------------+-------------------+--------------+
| val | replace_beginning | replace_end |
+--------------+-------------------+--------------+
| DadaLalaDada | DuduLalaDada | DadaLalaDudu |
| BlahDadaBlah | BlahDadaBlah | BlahDadaBlah |
+--------------+-------------------+--------------+
I recently recoded one of my sites, and the database structure is a little bit different.
I'm trying to convert the following:
*----*----------------------------*
| id | file_name |
*----*----------------------------*
| 1 | 1288044935741310953434.jpg |
*----*----------------------------*
| 2 | 1288044935741310352357.rar |
*----*----------------------------*
Into the following:
*----*----------------------------*
| id | file_name |
*----*----------------------------*
| 1 | 1288044935741310953434 |
*----*----------------------------*
| 2 | 1288044935741310352357 |
*----*----------------------------*
I know that I could do a foreach loop with PHP, and explode the file extension off the end, and update each row that way, but that seems like way too many queries for the task.
Is there any SQL query that I could run that would allow me to remove the file exentision from each field in the file_name column?
You can use the REPLACE() function in native MySQL to do a simple string replacement.
UPDATE tbl SET file_name = REPLACE(file_name, '.jpg', '');
UPDATE tbl SET file_name = REPLACE(file_name, '.rar', '');
This should work:
UPDATE MyTable
SET file_name = SUBSTRING(file_name,1, CHAR_LENGTH(file_name)-4)
This will strip off the final extension, if any, from file_name each time it is run. It is agnostic with respect to extension (so you can have ".foo" some day) and won't harm extensionless records.
UPDATE tbl
SET file_name = TRIM(TRAILING CONCAT('.', SUBSTRING_INDEX(file_name, '.', -1) FROM file_name);
You can use SUBSTRING_INDEX function
SUBSTRING_INDEX(str,delim,count)
Where str is the string, delim is the delimiter (from which you want a substring to the left or right of), and count specifies which delimiter (in the event there are multiple occurrences of the delimiter in the string)
Example:
UPDATE table SET file_name = SUBSTRING_INDEX(file_name , '.' , 1);