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), ':');
Related
I have the following lines in an SQL database, and I would like to find a query to delete from <_string> all the way up to </_string> in all the DB. Someone can help with that?
<game_machine>
<_string>
<savedgamedata><![CDATA[{"m_sceneName":"AGF_Demoopen","m_list":[{"key":"DailyQuestsBoardLv2","sceneIndex":-1,"data":"{\"staticQuestIds\":[],\"staticQuestData\":[],\"proceduralQuests\":[],\"deletedStaticQuests\":[]}"},"{\"staticQuestIds\":[],\"staticQuestData\":[],\"proceduralQuests\":[],\"deletedStaticQuests\":[]}"}]}]]></savedgamedata>
</_string>
</game_machine>
You can use regexp_replace():
select regexp_replace(col, '<_string>.*</_string>', '', 1, 1, 'n')
from (select '<game_machine>
<_string>
<savedgamedata><![CDATA[{"m_sceneName":"AGF_Demoopen","m_list":[{"key":"DailyQuestsBoardLv2","sceneIndex":-1,"data":"{\"staticQuestIds\":[],\"staticQuestData\":[],\"proceduralQuests\":[],\"deletedStaticQuests\":[]}"},"{\"staticQuestIds\":[],\"staticQuestData\":[],\"proceduralQuests\":[],\"deletedStaticQuests\":[]}"}]}]]></savedgamedata>
</_string>
</game_machine>' as col) t;
The 'n' parameter allows the . to match newline characters.
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)
In my table I have a record such as => Base\'ball.
When I execute SELECT query it fetch that column value as same => Base\'ball.
But I want that value to appear as Baseball.
So how can I do that in sql query?
try this Query:
select REPLACE(your_field, 'Base\\\'ball', 'Baseball')
WHERE your_field LIKE '%Base\'ball%'
Update:
select REPLACE(your_field, '\'', '')
WHERE your_field LIKE '%\'%'
try instead of doing that in query.
fetch result and apply mechanism of replace on that.
$baseball = "Base\'ball.";
echo $baseball = str_replace("\'","",$baseball);
working perfactly...
Try using the replace
Replace will work like below,
select column_name from table_name REPLACE(column_name, '\\\'', '');
You can use one (or some combination) of MySQL's string functions.
E.g., to get you started...
SELECT SUBSTRING_INDEX('this-is-example-string','example',1);
Hope it helps.
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.
Beginning with a letter I know how to do:
WHERE mov_title REGEXP CONCAT('^(the )?', '$letter')
And this method will work if I substitute $letter with any number, so if its set to 1, it will find all records that begin with 1, but I need it to work for any number 0-9. How could I modify the query?
WHERE mov_title REGEXP '^(the )?[0-9]'
(Or set $letter to [0-9] if you want to keep using your existing WHERE clause.)
Another option may be to use the substring function
WHERE substring( post_title, 1, 1 ) between '0' and '9'
Perhaps SQL Wild cards [charlist] might help:
http://w3schools.com/sql/sql_wildcards.asp