Related
working on a code where we are storing images but some images ending with weird characters
like , %2C -x1 to x10 etc or more but always end wih a .jpg
how can i regex to replace the image name to be a valid name
here is an example of what i have
PCpaste_10_g,-X1,-X2,-X3
SNBar_NEW,-X1
they can go till -X10
so i want to have regex to remove ,and everything afterwards it
i tried using replace but that only works for one item at a time
If your data is consistent with the string before the first comma that need to be taken, then you can try with SUBSTRING_INDEX;
Let's use this as you sample table & using your sample data:
CREATE TABLE mytable (
val VARCHAR(255));
INSERT INTO mytable VALUES
('PCpaste_10_g,-X1,-X2,-X3.jpg'),
('SNBar_NEW,-X1.jpg');
val
PCpaste_10_g,-X1,-X2,-X3.jpg
SNBar_NEW,-X1.jpg
Then first you extract the first string before comma occurrence:
SELECT SUBSTRING_INDEX(val,',',1) extracted
FROM mytable
returns
extracted
PCpaste_10_g
SNBar_NEW
Then to add back .jpg:
SELECT CONCAT(SUBSTRING_INDEX(val,',',1),'.jpg') extracted_combined
FROM mytable
IF your image extension is not consistently .jpg, you can do another SUBSTRING_INDEX() to get the extension then CONCAT() them:
SELECT CONCAT(SUBSTRING_INDEX(val,',',1) ,'.',
SUBSTRING_INDEX(val,'.',-1)) Extracted_combined
FROM mytable;
Demo fiddle
You can use LOCATE to find the first occurrence of "," in the field and LEFT to grab everything up to the first "," -
SET #value := 'PCpaste_10_g,-X1,-X2,-X3';
SELECT CONCAT(LEFT(#value, LOCATE(',', #value) - 1), '.jpg');
or for your update -
UPDATE <table>
SET image_name = CONCAT(LEFT(image_name, LOCATE(',', image_name) - 1), '.jpg')
WHERE image_name LIKE '%,%';
or to handle your %2C at the same time -
UPDATE <table>
SET image_name = CASE
WHEN image_name LIKE '%,%'
THEN CONCAT(LEFT(image_name, LOCATE(',', image_name) - 1), '.jpg')
WHEN image_name LIKE '%\%2C%'
THEN CONCAT(LEFT(image_name, LOCATE('%2C', image_name) - 1), '.jpg')
END
WHERE image_name LIKE '%,%'
OR image_name LIKE '%\%2C%';
I have a field with value like this 'DOOR-LEFT' and I want to change this to 'Door-LEFT'.
I came across this query on this site:
UPDATE tbl
SET field1 = CONCAT(UCASE(LEFT(field1, 1)),
LCASE(SUBSTRING(field1, 2)));
The above query changes 'DOOR-LEFT' to 'Door-left'. I do not want anything after the - to be updated. So it should be 'Door-LEFT'.
How can I do this?
You can use sustring_index() to split the string on '-', and then use the logic you already have at hand:
update tbl
set field = concat(
upper(left(substring_index(field1, '-', 1), 1)),
lower(substr(substring_index(field1, '-', 1), 2)),
'-',
upper(substring_index(field1, '-', -1))
)
The surrounding upper() in the last part of the string is not strictly needed for your sample string, which is all upper case to start with. I left it on purpose in case it might be useful for other cases (and it doesn't hurt anyway).
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'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 :).