UPDATE TABLE split() in mysql - mysql

How would I do the following:
code code_base
en-DE en
yue-mn yue
en en
In python, I would do:
code_base = code.split('-')[0]
How would I do the equivalent in mysql, in pseudocode:
UPDATE table SET code_base=code.split('-')[0]

You can use the SUBSTRING_INDEX to perform a split:
UPDATE table
SET code_base= SUBSTRING_INDEX(code, '-', 1)
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index

There's no way to "split" a string in MySQL. Splitting usually returns an array, which MySQL doesn't have.
What you can do is, get the substring from 0 up until the first -. Check out SUBSTRING_INDEX:
UPDATE table SET code_base=SUBSTRING_INDEX(code, '-', 1)

Try this:
UPDATE table SET code_base= LEFT(code, LOCATE('-', code)-1)

Related

Remove part of string in table

I have a table where one field named "version" contains the string "MyProgram nnnnnn".
I now wish to replace these strings so that they are only "nnnnnn", thereby remove the prepending "MyProgram ".
Is this possible, and if so, how would I do this?
If the pattern is "MyProgram nnnnnn" maning string like VB 1.3, Mysql 5.6, PHP 5.4 etc then you can do the following
update tablename
set col = substring_index(col,' ',-1)
Use Replace function,
SELECT VERSION,
REPLACE(VERSION,'MyProgram nnnnnn','nnnnnn')
FROM FROM tablename
Check this Manual
SELECT REPLACE(VERSION, 'MyProgram ', '')
FROM tablename
You can use the Replace() function for MySQL
update table set columnname = REPLACE(columnname, 'MyProgram ', '');
You want:
I now wish to replace these strings so that they are only "nnnnnn"
Shortest solution:
SELECT 'nnnnnn'
Specify your pattern of the content of this column!
SELECT VERSION,
REPLACE(VERSION,'MyProgram nnnnnn','nnnnnn')
FROM FROM tablename
Regards,
Praveen Nelge

query to remove all characters after last comma in string

i have a mysql table with this sort of data
TACOMA, Washington, 98477
Now i have thousands of such rows. I want the data to be manipulated in such a manner that it appears like:
TACOMA, Washington
Is it possible though mysql or do i have to manually do it.
You can use :
SELECT SUBSTRING_INDEX('TACOMA, Washington, 98477', ',', 2)
You can read more here.
And the update statement :
UPDATE my_table
SET my_col = SUBSTRING_INDEX(my_col, ',', 2)
Where you need to replace my_table with your table name and my_col with the column you need to be updated.
Possibly this way. Count the number of commas (by checking the length against the length with all the commas removed) and then use SUBSTRING_INDEX to get the string up to the number of commas:-
SELECT SUBSTRING_INDEX(col, ',', LENGTH(col) - LENGTH(REPLACE(col, ',', '')))
FROM SomeTable
substring_index(col, ',',-1)
will give the string from last index of comma to end of string
replace(col,concat(',',substring_index(col, ',',-1)),'')

Remove percentage sign from values in MySQL table

I have a lot of rows with values with percentage sign that I would like to remove from a column.
Is there a good SQL query to achieve this?
Use the REPLACE function:
UPDATE YourTable
SET YourColumn = REPLACE(YourColumn, '%', '');
update your_table set your_column = replace(your_column, '%', '')
If your DBMS does NOT have a "replace" function, you will have to use character substitution using several string functions.
Here is an example in Sybase and SQL Server.
UPDATE YourTable
SET YourColumn = stuff(YourColumn, patindex(YourColumn, '%'), 1, NULL)
This code will find the pattern of '%' in YourColumn, then use that position number to replace the character with NULL.

mysql specific substring

I have a about 500.000 rows in database and they are look like:
":hello:hi:"
":bye:good bye:bye-bye:"
":hi:hi-hi:hi-hi-hi:hallo:"
So, i need to update my table and remove everything after third character ':' in value, so my result should be:
":hello:hi:"
":bye:good bye:"
":hi:hi-hi:"
Is it possible? Thx.
MySQL 4.1.22.
Yes, the function SUBSTRING_INDEX does this:
UPDATE `table` SET `field` = CONCAT(SUBSTRING_INDEX(`field`, ':', 3), ':');

MySQL Update command

i need to add a special text to all rows in my mysql table ,
how to add some text to the end of all rows' content in a table just for one field
i used this code :
UPDATE `blogs` SET `title`= `title` + 'mytext';
but didnt work for me
UPDATE blogs SET title=concat(title, 'mytext');
MySQL does not have a string concatenation operator (+). You have to use the concat() function, as Daniel Schneller pointed out in the other answer.
If you try SELECT '1' + '2'; in MySQL, it would return 3. The + operator is simply an addition operator. Your query would have updated the title field with a 0.