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
Related
I have a column with links in my table. Some links have a string like /?source=rss&....
So I want to remove this string /?source=rss and what comes after it.
So if the link looks like this https://website.com/?source=ress&id=123&name=john
It should be https://website.com
I searched about it but only found replace and remove functions, And they won't work as the string is variable and not the same in every link.
I found this UPDATE MyTable SET column = REPLACE(column, ' ', '') WHERE column LIKE '%%'
The database is MYSQL and I'm using SQL queries there
Use substring_index():
UPDATE MyTable
SET column = SUBSTRING_INDEX(column, '/?', 1)
WHERE column LIKE '%/?%';
I have values stored like this in a field 1,255,230,265.
Is there a function in MySQL that will give me the second value in that string? In this case it'll be 255.
I tried using locate, but that does not seem to be meant for this.
Try this
select SUBSTRING_INDEX(SUBSTRING_INDEX(field_name,',',2),",",-1) from table_name
You might want to use SUBSTRING_INDEX() function.
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(field,',',2),',',-1)
FROM yourTable.
This grabs everything infront of the second comma, then grabs everything after the last comma (-1)
Try This
select * from Table1 where ',' + Nos+ ',' like '%,255,%'
Refer:
MySQL query finding values in a comma separated string
mysql check if numbers are in a comma separated list
Use FIND_IN_SET() function:
SELECT *
FROM tableA
WHERE FIND_IN_SET(255, columnName)
OR
Use LIKE operator
SELECT *
FROM tableA
WHERE CONCAT(',', columnName, ',') LIKE '%,255,%'
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.