Trying to find all field starting with space - mysql

My DB is acting a little weird and I figured out that coin_id of some products has a "space" as their first letter. So instead of "123456" it is " 123456". I am trying to find all the coin_id that start with a "space" and then remove the spaces.

No need to find them yourself, just trim them in an UPDATE query. Use LTRIM or just TRIM if there shouldn't be leading or trailing spaces:
UPDATE table_name SET coin_id = LTRIM(coin_id)
As Dudu Markovitz points out, especially if there are few to update of many, you can find them and update them for possibly better performance:
UPDATE table_name SET coin_id = LTRIM(coin_id) WHERE coin_id LIKE ' %'
Or for TRIM:
UPDATE table_name SET coin_id = TRIM(coin_id)
WHERE coin_id LIKE ' %' OR coin_id LIKE '% '

Update has performance Implications.
Update only what you need to update.
update t
set coin_id = ltrim(coin_id)
where coin_id like ' %'

How about this?
Removing one character from the columns starting with a space
UPDATE table
SET coin_id = RIGHT(coin_id,LENGTH(coin_id)-1)
WHERE LEFT(coin_id,1) = ' ';

First, why is an id that consists of digits stored as a string? Perhaps the "12345" is not really a representative value.
I would suggest that you use like:
update t
set coin_id = trim(coin_id)
where coin_id like ' %';
Notes:
like ' %' allows the optimizer to use an index on t(coin_id).
The query uses trim(). After spaces, at the end could also be a problem.
If the value is digits, then it should be stored as a number of some sort.

Related

Replacint part of string from a certain point

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.

Add period or punctuation to end of records in MYSQL?

I've got a column in my database with descriptions. When the data was entered some of the descriptions are missing a period at the end and I would like to update the rows and add them if they don't exist.
Is the best approach to use the REGEXP function or is there a better way?
EDIT :
This ended up being more complex than I had originally thought so I wanted to share this process for anyone else that may find it useful.
The first complication was that I had records with whitespace and other characters at the end of some strings, which I took care of like this.
UPDATE table_name SET col_name = REPLACE(TRIM(TRAILING ' ' FROM col_name),
TRIM(TRAILING '\r' FROM col_name),
TRIM(TRAILING '\n' FROM col_name))
The second issue was excluding several other punctuation marks when adding the period. So here is the final query.
UPDATE table_name
SET col_name = CONCAT(col_name,'.')
WHERE RIGHT(col_name,1) <> '.'
AND RIGHT(col_name,1) <> '!'
AND RIGHT(col_name,1) <> '?'
Just use update and like:
update table t
set col = concat(col, '.')
where col not like '%.';
EDIT:
For multiple types of punctuation, you could use a regular expression, but I would just do:
update table t
set col = concat(col, '.')
where right(col, 1) not in ('.', ';', '!', '?', . . .);
You can use right function https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_right
mysql> SELECT RIGHT('foobarbar', 1);
+-----------------------+
| RIGHT('foobarbar', 1) |
+-----------------------+
| r |
+-----------------------+
1 row in set (0.00 sec)
mysql> SELECT RIGHT('foobarbar.', 1);
+------------------------+
| RIGHT('foobarbar.', 1) |
+------------------------+
| . |
+------------------------+
1 row in set (0.00 sec)
So may be you can do as
update table_name
set col_name = concat(col_name,'.')
where right(col_name,1) <> '.'
UPDATE table
SET description=IF(RIGHT(description,1)<>'.', description, CONCAT(description, '.'));
Basically, this just updates every value in the description field; if it ends in a period, it just inserts the same value (ie. doesn't change anything). If it doesn't end in a period, it concatenates one onto the end.
REGEXP '[.!?][[:space:]]*$'
That is 'true' for trailing punctuation optionally followed by zero or more whitespace characters, including space, CR, and LF.
And this is probably more efficient than any other technique because it is looking at each row only once (no ANDs or ORs). (Yes, REGEXP is generally slower than LIKE. But this is a case where one REGEXP is doing the work of several LIKEs/RIGHTs/etc.)
Sure, you might want to pre-TRIM the data. That cuts my 'answer' down to
WHERE col_name NOT REGEXP '[.!?]$'

mysql Setting field with multiple column as comma separated value

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)

Trim spaces from values in MySQL table

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)

MySQL replace all whitespaces with -

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.