Specifically I need to pick the part of field label from table tmp.label between delimiters <!-- rwbl_1 --> and <!-- rwbl_2 --> where label contains <span in order to be able to update that field for records erroneously formatted with HTML tags.
The result amounts to something like strip_tags. This is, obviously, only possible due to the presence of the abovementioned (or similar) delimiters.
Here is a simple solution for the abovementioned case to extract a substring given 2 delimiters.
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(label,
'<!-- rwbl_1 -->', -1),
'<!-- rwbl_2 -->', 1) AS cutout
FROM tmp.label
WHERE label LIKE '<span%'
ORDER BY 1
LIMIT 1;
More abstract:
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(string_field, # field name
'delimiter_1', -1), # take the right part of original
'<!-- delimiter_2 -->', 1) # take the left part of the resulting substring
AS cutout
FROM my_table
WHERE my_condition;
And the update now works like this:
UPDATE my_table
SET string_field = SUBSTRING_INDEX(
SUBSTRING_INDEX(string_field,
'delimiter_1', -1),
'<!-- delimiter_2 -->', 1)
WHERE my_condition;
Let's say that I have a record like https://stackoverflow.com//questions/, somehow this happened and I want to update the record as https://stackoverflow.com/questions/.
So far I thought of replacing every occurrence of // but this would break https:// part as http:/ too.
I have identified all the broken records via:
SELECT TRIM(TRAILING SUBSTRING_INDEX(url, '//', -1) FROM url) FROM table_name WHERE length(TRIM(TRAILING SUBSTRING_INDEX(url, '//', -1) FROM url)) > 8;
This checks after 8 characters, so that all http:// and https:// occurrences are skipped. There're currently 302 URLs in this situation.
How can I solve this issue?
Replace twice.
So the problem is that also https:// becomes https:/ ?
Just means you need to add that 1 lost slash again.
UPDATE yourtable
SET url = REPLACE(REPLACE(url,'//','/'),':/','://')
WHERE url LIKE '%://%//%'
Check this:
SET #val = 'https://stackoverflow.com//questions/';
select
concat(
substr(#val, 1, instr(#val, '//') + 1),
replace(substr(#val, instr(#val, '//') + 2),'//', '/')
)
it replaces all occurrences of // after the 1st with /
See the demo
So you can use it in an update:
update tablename
set mycolumn = concat(
substr(mycolumn, 1, instr(mycolumn, '//') + 1),
replace(substr(mycolumn, instr(mycolumn, '//') + 2),'//', '/')
)
I have a column which returns
a:2:{i:0;s:10:"Properties";i:1;s:14:"Movable Assets";}
I would like to return only:
Properties, Movable Assets
How can I use a select statement to retrieve the values between the " symbols
these are serialize values, you can use php, to get your desired results.
you can use , unserialize which will return an array then you can use implode to get the comma separated values.
example
Use SUBSTRING_INDEX().
SUBSTRING_INDEX() takes a string argument followed by a delimiter character and the number of parts to return. After you break up the string using the delimiter, that number of parts is returned as a single string.
select concat(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
'a:2:{i:0;s:10:"Properties";i:1;s:14:"Movable Assets";}',
'"',
4
),
'"',
2
),
'"',
-1
),
",",
SUBSTRING_INDEX(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
'a:2:{i:0;s:10:"Properties";i:1;s:14:"Movable Assets";}',
'"',
4
),
'"',
4
),
'"',
-1
)
);
Use combination of LOCATE() and SUBSTRING().
Definitions: https://dev.mysql.com/doc/refman/5.0/en/string-functions.html
Or better -- migrate the data to actually be retrievable.
I'm don't have a lot of knowledge of MySql (or SQL in general) so sorry for the noobness.
I'm trying to update a bunch of String entries this way:
Lets say we have this:
commands.firm.pm.Stuff
Well I want to convert that into:
commands.firm.pm.print.Stuff
Meaning, Add the .print after pm, before "Stuff" (where Stuff can be any Alphanumerical String).
How would I do this with a MySql Query? I'm sure REGEXP has to be used, but I'm not sure how to go about it.
Thanks
Try something like this. It finds the last period and inserts your string there:
select insert(s, length(s) - instr(reverse(s), '.') + 1, 0, '.print')
from (
select 'commands.firm.pm.Stuff' as s
) a
To update:
update MyTable
set MyColumn = insert(MyColumn, length(MyColumn) - instr(reverse(MyColumn), '.') + 1, 0, '.print')
where MyColumn like 'commands.firm.pm.%'
Perhaps use a str_replace to replace commands.firm.pm to commands.firm.pm.print
$original_str = "commands.firm.pm.15hhkl15k0fak1";
str_replace("commands.firm.pm", "commands.firm.pm.print", $original_str);
should output: commands.firm.pm.print.15hhkl15k0fak1
then update your table with the new value...How to do it all in one query (get column value and do the update), I do not know. All I can think of is you getting the column value in one query, doing the replacement above, and then updating the column with the new value in a second query.
To update rows that end in '.Stuff' only:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column) - CHAR_LENGTH('.Stuff') )
, '.print'
, '.Stuff'
)
WHERE Column LIKE '%.Stuff'
To update all rows - by appending .print just before the last dot .:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column)
- CHAR_LENGTH(SUBSTRING_INDEX(Column, '.', -1))
)
, 'print.'
, SUBSTRING_INDEX(Column, '.', -1)
)
WHERE Column LIKE '%.%'
I am trying to extract a certain part of a column that is between delimiters.
e.g. find foo in the following
test 'esf :foo: bar
So in the above I'd want to return foo, but all the regexp functions only return true|false,
is there a way to do this in MySQL
Here ya go, bud:
SELECT
SUBSTR(column,
LOCATE(':',column)+1,
(CHAR_LENGTH(column) - LOCATE(':',REVERSE(column)) - LOCATE(':',column)))
FROM table
Yea, no clue why you're doing this, but this will do the trick.
By performing a LOCATE, we can find the first ':'. To find the last ':', there's no reverse LOCATE, so we have to do it manually by performing a LOCATE(':', REVERSE(column)).
With the index of the first ':', the number of chars from the last ':' to the end of the string, and the CHAR_LENGTH (don't use LENGTH() for this), we can use a little math to discover the length of the string between the two instances of ':'.
This way we can peform a SUBSTR and dynamically pluck out the characters between the two ':'.
Again, it's gross, but to each his own.
This should work if the two delimiters only appear twice in your column. I am doing something similar...
substring_index(substring_index(column,':',-2),':',1)
A combination of LOCATE and MID would probably do the trick.
If the value "test 'esf :foo: bar" was in the field fooField:
MID( fooField, LOCATE('foo', fooField), 3);
I don't know if you have this kind of authority, but if you have to do queries like this it might be time to renormalize your tables, and have these values in a lookup table.
With only one set of delimeters, the following should work:
SUBSTR(
SUBSTR(fooField,LOCATE(':',fooField)+1),
1,
LOCATE(':',SUBSTR(fooField,LOCATE(':',fooField)+1))-1
)
mid(col,
locate('?m=',col) + char_length('?m='),
locate('&o=',col) - locate('?m=',col) - char_length('?m=')
)
A bit compact form by replacing char_length(.) with the number 3
mid(col, locate('?m=',col) + 3, locate('&o=',col) - locate('?m=',col) - 3)
the patterns I have used are '?m=' and '&o'.
select mid(col from locate(':',col) + 1 for
locate(':',col,locate(':',col)+1)-locate(':',col) - 1 )
from table where col rlike ':.*:';
If you know the position you want to extract from as opposed to what the data itself is:
$colNumber = 2; //2nd position
$sql = "REPLACE(SUBSTRING(SUBSTRING_INDEX(fooField, ':', $colNumber),
LENGTH(SUBSTRING_INDEX(fooField,
':',
$colNumber - 1)) + 1)";
This is what I am extracting from (mainly colon ':' as delimiter but some exceptions), as column theline255 in table loaddata255:
23856.409:0023:trace:message:SPY_EnterMessage (0x2003a) L"{#32769}" [0081] WM_NCCREATE sent from self wp=00000000 lp=0023f0b0
This is the MySql code (It quickly did what I want, and is straight forward):
select
time('2000-01-01 00:00:00' + interval substring_index(theline255, '.', 1) second) as hhmmss
, substring_index(substring_index(theline255, ':', 1), '.', -1) as logMilli
, substring_index(substring_index(theline255, ':', 2), ':', -1) as logTid
, substring_index(substring_index(theline255, ':', 3), ':', -1) as logType
, substring_index(substring_index(theline255, ':', 4), ':', -1) as logArea
, substring_index(substring_index(theline255, ' ', 1), ':', -1) as logFunction
, substring(theline255, length(substring_index(theline255, ' ', 1)) + 2) as logText
from loaddata255
and this is the result:
# LogTime, LogTimeMilli, LogTid, LogType, LogArea, LogFunction, LogText
'06:37:36', '409', '0023', 'trace', 'message', 'SPY_EnterMessage', '(0x2003a) L\"{#32769}\" [0081] WM_NCCREATE sent from self wp=00000000 lp=0023f0b0'
This one looks elegant to me. Strip all after n-th separator, rotate string, strip everything after 1. separator, rotate back.
select
reverse(
substring_index(
reverse(substring_index(str,separator,substrindex)),
separator,
1)
);
For example:
select
reverse(
substring_index(
reverse(substring_index('www.mysql.com','.',2)),
'.',
1
)
);
you can use the substring / locate function in 1 command
here is a mice tutorial:
http://infofreund.de/mysql-select-substring-2-different-delimiters/
The command as describes their should look for u:
**SELECT substr(text,Locate(' :', text )+2,Locate(': ', text )-(Locate(' :', text )+2)) FROM testtable**
where text is the textfield which contains "test 'esf :foo: bar"
So foo can be fooooo or fo - the length doesnt matter :).