Removing part of a string in column - mysql

I am trying to remove part of a string. But can't do it properly. String is like this: 4,290TL〜9,490TL So trying to remove after this 〜 
I tried
UPDATE SET price = SUBSTRING_INDEX(price, '〜')
But not worked.

SUBSTRiNG_INDEX requires 3 parameters, the last one being the delimiter count. In this case you need to supply a count of 1, indicating that you want everything to the left of the first occurrence of the delimiter 〜. Additionally, you need to specify your table name in the query. Try this:
UPDATE yourtable SET price = SUBSTRING_INDEX(price, '〜', 1)

UPDATE SET price = SUBSTRING_INDEX(price, '〜', 1)

Please note the the strings you have shared with this question or comments uses DIFFERENT wavy line ("tilde") characters
# the tilde character used here is DIFFERENT to the next example
select substring_index('4,290TL〜9,490TL','〜',1)
;
+----+------------------------------------------------------+
| | substring_index('4,290TL〜9,490TL','〜',1) |
+----+------------------------------------------------------+
| 1 | 4,290TL |
# the tilde character is different to the one above
select substring_index('18,990万円(1戸)~28,790万円(1戸)','~',1)
;
+----+-----------------------------------------------------------+
| | substring_index('18,990万円(1戸)~28,790万円(1戸)','~',1) |
+----+-----------------------------------------------------------+
| 1 | 18,990万円(1戸) |
You will need to be CERTAIN the the tilde you use as delimiter is the one you use in substring_index() otherwise that function will just return the whole string.

Related

Upadate mysql database text using a query

We currently have a database that has stored over 100,000 records of data over the years however in a structure that does not work anymore.
There is a field in the table called youtube_video
It has been storing all of the embed YouTube videos like this:
http://www.youtube.com/embed/3mHuu5NklOs?rel=0
http://www.youtube.com/embed/3mHuu5NklOs
We need to change it to:
https://www.youtube.com/watch?v=3mHuu5NklOs
Is there a way to write a query that makes this change with a single query?
You can use REGEXP_REPLACE :
SELECT REGEXP_REPLACE(
youtube_video,
'^http://www.youtube.com/embed/([^?]+).*',
'https://www.youtube.com/watch?v=\1'
) FROM mytable
Regex breakdown :
^ : start of string
http://www.youtube.com/embed/ : constant string part
([^?]+) : as many consecutive characters as possible others than a question mark ; the surrounding parentheses capture that part of the string, and make it available as \1 in the second argument to REGEXP_REPLACE()
.* : anything (until end of string)
This demo on DB Fiddle returns :
| youtube_video | new_youtube_video |
| ---------------------------------------------- | ------------------------------------------- |
| http://www.youtube.com/embed/3mHuu5NklOs?rel=0 | https://www.youtube.com/watch?v=3mHuu5NklOs |
| http://www.youtube.com/embed/3mHuu5NklOs | https://www.youtube.com/watch?v=3mHuu5NklOs |
If needed, you can easily turn this into an UPDATE :
UPDATE mytable
SET youtube_video = REGEXP_REPLACE(
youtube_video,
'^http://www.youtube.com/embed/([^?]+).*',
'https://www.youtube.com/watch?v=\1'
);

MySQL delete lines that contains specific word

I'm trying to delete lines in specific column from all rows that contains specific words.
For example:
Remove lines that contain word apple and it is always at the beginning of the line.
+--+------------------+
|ID|data |
+--+------------------+
|1 |sometext1 |
| |sometext2 |
| |apple sometext3 |
| |sometext4 |
+--+------------------+
|2 |apple sometext5 |
| |sometext6 |
+--+------------------+
so the result would be:
+--+------------------+
|ID|data |
+--+------------------+
|1 |sometext1 |
| |sometext2 |
| |sometext4 |
+--+------------------+
|2 |sometext6 |
+--+------------------+
'SometextX' is different in every line, number of lines is different in every row and it has different number of characters in every line.
I really need this in MySQL any help would be appreciated.
You would be better off using REGEXP here to match patterns in each line:
DELETE FROM yourTable WHERE text REGEXP '^apple';
REGEXP allows for fairly complex regex matching, and would be useful if your requirement changes or gets more complex later on.
Edit: MySQL has no built in support for regex replacement, so there is no easy way to accomplish what you want.
A general regex pattern to remove the word apple would be \bapple\b. You may search on this pattern and replace with empty string.
You would use where:
where textcol not like 'apple%' or textcol is null
This can be part of a select or a delete (the question mentions "result" which suggests the former and "delete" which suggests the latter). It is not clear whether you actually want to change the data or whether you just want the result set without these words.
Note: you can do this without or and still handle NULL values, because MySQL has a NULL-safe equality operator:
where not left(textcol, 5) <=> 'apple'
You can use MySQL functions to select the right rows and to update with new data as follows:
UPDATE `yourTable` SET `yourField` = REPLACE(yourField, 'apple', '') WHERE yourField LIKE '%apple%'
If you don't want to delete the whole row, you can run these 3 queries in this order
update your_table set text=replace(text,substring(text,#start:=locate('\napple',text),locate('\n',text,#start+1)-#start+1),'');
update your_table set text=if((#start:=locate('apple',text))=1,replace(text,substring(text,#start,locate('\n',text,#start+1)-#start+1),''),text);
update your_table set text=if((#start:=locate('apple',text))=1,replace(text,substring(text,locate('apple',text)),''),text);
update #1 will remove apple in the middle of the text (prefixed by \n)
update #2 will remove apple at the beginning of its row (nothing before) and having following rows
update #3 will remove remaining cases

How to match hyphen delimited in any order

I need to match a set of characters delimited by a hyphen - for example:
B-B/w-W/Br-W-Br
Where the / are part of what I need, up to 20 spaces.
G-R-B, G/R-B-B/W-O
So I need a regex that covers between the -'s in any order (G-R-B could also be R-B-G)
I've been playing around with a bunch of combo's, but I can't come up with something that will match any order.
The plan is to search this way using mysql. So, it'll be something like
select * from table1 where pinout REGEXP '';
I just can't get the regex right :/
Description
This expression will match the string providing each of the hyphen delimited values are included in the string. The color values can appear in the string in any order so this expression will match W/Br-b-B/w and B/w-W/Br-b... or any other combinations which include those colors.
^ # match the start to of the string
(?=.*?(?:^|-)W\/Br(?=-|$)) # require the string to have a w/br
(?=.*?(?:^|-)b(?=-|$)) # require the string to have a b
(?=.*?(?:^|-)B\/w(?=-|$)) # require the string to have a b/w
.* # match the entire string
MySql doesn't really support the look arounds so this will need to be broken into a group of where statements
mysql> SELECT * FROM dog WHERE ( color REGEXP '.*(^|-)W\/Br(-|$)' and color REGEXP '.*(^|-)b(-|$)' and color REGEXP '.*(^|-)B\/w(-|$)' );
+-------+--------+---------+------+------------+---------------------+
| name | owner | species | sex | birth | color |
+-------+--------+---------+------+------------+---------------------+
| Claws | Gwen | cat | m | 1994-03-17 | B-B/w-W/Br-W-Br |
| Buffy | Harold | dog | f | 1989-05-13 | G-R-B, G/R-B-B/W-O |
+-------+--------+---------+------+------------+---------------------+
See also this working sqlfiddle: http://sqlfiddle.com/#!2/943af/1/0
Using a regex in conjunction with a MySql where statement can be found here: http://dev.mysql.com/doc/refman/5.1/en/pattern-matching.html
I might have misunderstood from your example, try this:
-*([a-zA-Z/]+)-*
The capture region can be altered to include your specific letters of interest, e.g. [GRBWOgrbwo/].
Edit: I don't think this will help you in the context you're using it, but I'll leave it here for posterity.

Merge two numerical string

MYSQL command:
UPDATE `tbl_objednavka` SET
`TOTAL` = '6300',
`EANS` = CAST('8433611369655;' AS char)+CAST(`EANS` AS char),
`COUNTS` = CAST('1;' AS char)+CAST(`COUNTS` AS char)
WHERE `ID_OBJEDNAVKA`=2;
_____________________________________
| | |
| EANS | COUNTS |
+-----------------+-----------------+
| | |
| 8433611364094 | 1 |
+-----------------+-----------------+
It for some weird reason '8433611369655;' dont merge strings but add one number to the other so I get something as this: 1.6867223e+13...
I need to get an array, so this: 8433611369655;8433611364094 in EANS and 1;1 in COUNTS
I can use php for this, but I would love to do this using SLQ only
You are using MySQL, so you want to use concat() rather than + for string concatenation.
Also, you should never convert values to char() without a length. In this case, though, I think varchar() would be a more appropriate type.
However, I would suggest this query:
UPDATE `tbl_objednavka`
SET `TOTAL` = '6300',
`EANS` = concat('8433611369655;', `EANS`),
`COUNTS` = concat('1;', `COUNTS`)
WHERE `ID_OBJEDNAVKA`=2;
You don't seem to need the casts at all. The types of EANS and COUNTS should be character to begin with, because you are assigning character values to them.
If they are numeric, then you need to alter the table so they can hold the values you want. In practice, I would suggest adding new columns in this case. Or, using a view to create the new columns.
UPDATE `tbl_objednavka` SET
`TOTAL` = '6300',
`EANS` = CONCAT('8433611369655;', CAST(`EANS` AS char)),
`COUNTS` = CONCAT('1;', CAST(`COUNTS` AS char))
WHERE `ID_OBJEDNAVKA`=2;
MySQL doesn't use + for string concatenation, you have to use the CONCAT() function.

Replacing text in a MySQL query?

I have a column in my table, lets call it thecolumn. The values in that column are either a number, like 100036077, or a number followed by a name, like that 35921 John Doe.
I want to replace the names with nothing and add 1000 to the 5 numbers in front of that name (35921 John Doe --> 100035921) in my select. How can I do that without using any additional libraries? There is no native regex replace for MySQL, right?
Thanks!
This is a bit easy... or am I missing something?
SELECT CONCAT('1000',SUBSTRING_INDEX('35921 John Doe',' ',1))x;
+-----------+
| x |
+-----------+
| 100035921 |
+-----------+
The strategy here is to:
Get the substring from the beginning of the string up to the first space (which is the limit between the numeric part and the textual part)
Add the string "1000" to the string resulting of 1.
Replace current value with the string resulting from 2.
UPDATE thetable SET thecolumn =
CONCAT('1000', SUBSTRING_INDEX(thecolumn, ' ', 1));