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.
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.
Got ids stored in DB with Json format like this
'["1454","474","545"]'
I can build list IDs :
SELECT replace
(replace(
replace(
replace('["1454","474","545"]','[','\'')
,']','\'')
,'"','')
,',','\',\'')
mySql returns '1454','474','545'
But when I try to list DB records from this build list of IDs :
SELECT col FROM table WHERE col in (REPLACE
(REPLACE(
REPLACE(
REPLACE('["1454","474","545"]','[','\'')
,']','\'')
,'"','')
,',','\',\''));
mySql says "0 records" even if I add a "SELECT" before the first "REPLACE"
Any help ?
Try below query, you need to add select in your in clause also:
SELECT col FROM table WHERE col in (select REPLACE
(REPLACE(
REPLACE(
REPLACE('["1454","474","545"]','[','\'')
,']','\'')
,'"','')
,',','\',\''));
Alas, you cannot use in with a comma delimited string. It takes a list of elements, but not within a string. So, this works as you expect:
where x in (1,2,3)
This does not work as you expect (although it does work as I expect0;
where x in ('1,2,3')
This looks for one value of x that is the string '1,2,3'.
The solution is to use the MySQL function find_in_set():
SELECT col
FROM table
WHERE find_in_set(col, REPLACE(REPLACE(REPLACE('["1454","474","545"]', '[','\''
), ']', '\''
), '"', ''
), ',', '\',\''
);
To be honest, though, you might be better off with something like:
where '["1454","474","545"]' like concat('%;', col, '&%')
my raw query look something like this-
UPDATE main,category,sub_category
SET main.biz_keyword = (category.category','sub_category.sub_cat_name','main.biz_keyword)
so the result something like main.biz_keyword='Doctor,General Physician,Physician'
I know this is wrong query but you got the Idea what I am looking for,
So my question is that I can do this by single query?
You might want to have a look at using CONCAT_WS(separator,str1,str2,...)
CONCAT_WS() stands for Concatenate With Separator and is a special
form of CONCAT(). The first argument is the separator for the rest of
the arguments. The separator is added between the strings to be
concatenated.
is this something you want to achieve?
Update TableName
set biz_keyword = category.category + ',' + sub_category.sub_cat_name + ',' + main.biz_keyword
Maybe you're looking for something like this?
UPDATE
main
SET
biz_keyword = CONCAT_WS(', ',
(SELECT category FROM category WHERE ... ),
(SELECT sub_cat_name FROM sub_category WHERE ... ),
biz_keyword)
Is there an easy way to replace all the text in a VARCHAR 255 column from "300-21-2" to "300-21-02" with one query?
Thank you.
This is basic SQL
UPDATE tablename
SET columnname = '300-21-02'
WHERE columnname = '300-21-2'
If the pattern is always the same NNN-NN-N then what you need is:
update tablex
set column = concat( substr(column,1,7), lpad(substr(column,8),2,'0') )
see it at fiddle:
http://sqlfiddle.com/#!2/f59fe/1
EDIT As the op showed the pattern
update tablex
set column = CONCAT(
substring_index(col, '-',1), '-',
lpad(substring_index(substring_index(col, '-',-2), '-', 1),2,'0'), '-',
lpad(substring_index(col, '-',-1), 2, '0') )
If you like to convert the first set like 300 to 00300 as your pattern you add the lpad as this: lpad(substring_index(col, '-',1),5,'0')
This should be a lot easier if mysql has support to regex replace, but as it hasnt you have to work with the strings:
from this value: '300-02-1'
from substring_index(col, '-',1) I'm getting: 300
from substring_index(substring_index(col, '-',-2), '-', 1) I'm getting 02 I did this because just put the substring_index(col, '-',2) gave me 300-02 so, i got it from right to left (-2) then i get the first
and substring_index(col, '-',-1) it bring me 1 because it gets the value from right to left
Then I just concatenate it all formatting the ones I want.