My database contains many + signs and i want to replace it with white-space....
What should be the query for that ?
update tbl set column = replace(column, '+', ' ')
This updates all '+'s in the column named "column" in the table named "tbl" to a single white space, one for each "+"
If you only need to select for display, then same function, e.g.
select replace(column, '+', ' ') column, other1, col3
from tbl
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘+’,‘ ’);
Documentation
For Oracle: REPLACE
Related
How to remove all spaces between a column field?. The spaces occur in the middle of the text so trim won't work and also replace is not working.
my code is
UPDATE temp_emp t1, master_employee t2
SET t1.lm= t2.emp_id
where REPLACE(t1.lm, ' ', '') = REPLACE(CONCAT(t2.first_name,'',t2.last_name), ' ', '');
for example when i run the query ,
select REPLACE(lm, ' ', '') AS concat from temp_emp1
i get the output as follows
concat
----------------------------------------
rick joe
james cole
albert Th
i want the output to be ;like this
concat
----------------------------------------
rickjoe
jamescole
albertTh
Without knowing the table structures and data, it is difficult for me to follow what you are doing. However, to accomplish the ouput of two concatenated columns is very straightforward.
Assume you have a table master_employee with just two columns and you want to output the FIRST and LAST names concatenated with no spaces in between. You simply use the function concat()for MySQL:
SELECT CONCAT(first_name, last_name)
from master_employee;
In Oracle, the concatenation is two pipes (||):
SELECT first_name || last_name
from master_employee;
Hope this helps.
If you want to update the existing column which has multiple spaces into one then this update query will be helpful:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', ' ');
If you don't want any spaces then this should work:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', '');
i have found some wrong text in 120 record in a table so i have in a varchar field:
'Name rubbish rubbish2 more rubbish'
i want to keep 'Name' en remove all after Name
i have tried this :
SELECT REPLACE(field_name, 'rubbish','') as test
from table
where field_name like '%rubbish%'
but this will ofcourse remove only 'rubbish' not the rest.
I think ther must be a way to remove everything after 5 digits!?
Txs
To remove everything after the first space character:
update mytable set
field_name = substr(field_name, 1, instr(field_name, ' '))
where field_name like '%rubbish%';
See SQLFiddle.
In MySQL, if you want to keep everything before the first space, then you can use substring_index():
update t
set col = substring_index(col, ' ', 1)
where col like '% %';
If you have some set pattern, such as the string 'rubbish', then you can use that. So, this keeps everything before "rubbish":
update t
set col = substring_index(col, 'rubbish', 1)
where col like '%rubbish%';
You can also use this logic in a SELECT statement:
select substring_index(col, 'rubbish', 1)
. . .
If the string does not contain "rubbish", then everything is returned.
I want to update all the values in a table by trimming all the leading and trailing spaces. I have tried the following queries but neither worked.
I know that it is possible to use TRIM with SELECT, but how can I use it with UPDATE?
UPDATES teams SET name = TRIM(name)
UPDATES teams SET name = TRIM(LEADING ' ' TRAILING ' ' FROM name)
You do not have to SELECT.
Try this -
UPDATE teams SET name = TRIM(name)
WHERE 1 = 1;
UPDATE teams SET name = TRIM(name)
That should work, it is semantically correct for MySQL.
If field contain new line character then
UPDATE Tablename SET colname= TRIM(TRAILING '\r' FROM colname)
This one solves a weird problem I was having when the TRIM option does not work:
UPDATE
teams
SET
name = TRIM(BOTH UNHEX('C2A0') FROM name)
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.
how could i remove ALL whitespaces from a row?
I see here alot of same question but all answers ar to use replace option. Replace will work only to strip one spaces, not all.
ex: a b c to become a-b-c
Thanks.
This can be achieved with the following MySQL Function:
SELECT REPLACE( table.field, ' ', '-' ) FROM table;
This should replace all the whitespace to a -
update image set path = REPLACE( image.path, ' ', '-' ) where path like '% %'
if you would like to update the path in mysql itself use the update for all rows which have spaces withe %20
Try this
replace('a b c',' ','-')
UPDATE table SET table.field = REPLACE( table.field, ' ', '-' );
This will update all the fields, replacing all spaces with hyphens. This will actually modify the data in the tables. Fokko's answer above will change only the data that is pulled, therefore not changing the actual data.