I have a column in MySql that contains a description of a products. Sometimes the description contain more the one space between words and I would like to turn it into one space so I've found it with this query:
SELECT * FROM `database`.`PRODUCTS`
WHERE `PRODUCTS`.`description` LIKE '% %'
and then repaired it by:
UPDATE `database`.`PRODUCTS`
SET `PRODUCTS`.`description` = REPLACE(`PRODUCTS`.`description`,' ',' ')
But it doesn't remove all the double spaces! There are some kind of "special" spaces with (I suspect) different ascii code - 0xA0,0xC2
How can I SELECT it and remove it?
Thanks
These are not 'special' spaces - they are not ascii codes - if they are in your database then you've got bugs in the code which put them there.
Why not just replace them?
....
SET PRODUCTS.description = REPLACE(
REPLACE(
REPLACE(PRODUCTS.description
,CHAR(160),' ')
, CHAR(194), ' ')
, ' ', ' ');
Related
I have a very specific question.
I have to build a SQL statement that builds a table where some columns are merged together. These columns shall be formatted with delimiters like '\n' or ' ' or ' - '. These delimiters shall be added only if the column before is not empty or null. This should prevent empty lines or unneeded delimiters.
Here is how I started:
SELECT
any_table.table_id,
CONCAT(any_table.text1, '\n', any_table.text2) AS text1_2,
FROM
any_table
WHERE any_table.use = 'true'
This code concats text1 and text2 as a new column text1_2 and uses a line feed as delimiter. The missing part is that line feed shall just be added if any_table.text1 is not null or empty.
Is there an elegant way in doing this with SQL?
thx
Some databases support a very handy function called concat_ws() which does exactly what you want:
CONCAT_WS('\n', NULLIF(any_table.text1, ''), NULLIF(any_table.text2, '')) AS text1_2,
In standard SQL, you can do:
TRIM(LEADING '\n' FROM CONCAT( '\n', || NULLIF(any_table.text1, ''),
'\n' || NULLIF(any_table.text2, '')
)
)
It is possible that your database supports neither of these constructs.
if you'r under SQL SERVER you can use,
SELECT id, CONCAT(colonne1 + ' - ', colonne2) FROM "table"
if you'r under Oracle : you shoul use || for concaténation like
SELECT id, CONCAT(colonne1 || ' - ', colonne2) FROM "table"
There are a lot of rows with multiple spaces in column title and I want to replace them with a single space.
update abc set title = REPLACE(title, " ", " ");
Nothing is replaced.
I'm using phpMyAdmin.
I noticed (clicking on the button Simulate query that my query is transformed into:
update abc set title = REPLACE(title, " ", " ");
so replace single space with single space.
Any help?
While checking this page it came to my attention that to replacing all the double spaces from the database you might have triple space or more on the single record.
The thing that the some solution didn't take in consideration.So you need to make sure that your statement replace them all. Doing one time or two time replacement of double space with single space might not cover all the corrupted data.
For example having a record value as 'A B C'; what you can do is:
first replace all the single space with open/closed characters
like <> , or [] or {}...
Then replace the back to back reversed order characters (closed/open)
with empty value, so all >< or ][ or }{ will be removed.
Final step is to restore the single spaces by replacing the remaining open/close
characters with single space, for example <> will be changed back to ' '
I always use something like following to fix my data:
UPDATE Table1 SET Column1 = REPLACE(REPLACE(REPLACE(Column1, ' ', '<>'), '><', ''),'<>',' ');
Number of consecutive space characters can either be odd or even. You can replace two space characters with one space character, and do a similar replace again on the modified string to cover all the odd/even cases.
UPDATE abc SET title = REPLACE(REPLACE(title, ' ', ' '), ' ', ' ');
Explanation:
2 spaces: First replace will convert to 1 space. Second replace will not modify further.
3 spaces: First replace will convert (2+1) spaces to (1+1). Second will convert (1+1 = 2) spaces to 1 space.
4 spaces: First replace will convert (2+2) spaces to (1+1). Second will convert (1+1 = 2) spaces to 1 space.
and so on...
DEMO:
mysql> select
-> dt.test_str,
-> REPLACE(REPLACE(dt.test_str, ' ', ' '), ' ', ' ') AS modified
-> FROM
-> (SELECT 'thi s is a weird string' AS test_str) AS dt ;
+--------------------------------+--------------------------+
| test_str | modified |
+--------------------------------+--------------------------+
| thi s is a weird string | thi s is a weird string |
+--------------------------------+--------------------------+
1 row in set (0.00 sec)
You can try the following SELECT example, with REGEXP_REPLACE is used. For example:
SELECT 'ab asd asd a qeqw q qwe qweqw qw' AS `text 1`, REGEXP_REPLACE('ab asd asd a qeqw q qwe qweqw qw', ' \+', ' ') AS `text 2`;
REGEXP_REPLACE documentation
You can use REGEXP_REPLACE in an UPDATE statement:
UPDATE abc SET title = REGEXP_REPLACE(title, ' \+', ' ');
Here is my approach
SELECT ARRAY_TO_STRING(ARRAY_AGG(VALUE::varchar),' ') FROM TABLE(flatten(split(REGEXP_REPLACE('','\n'),' '))) WHERE VALUE <> '' ORDER BY INDEX;
might be a long route but does the job.
I would like to remove the trailing spaces from the expressions in my column and add them to beginning of the expression. So for instance, I currently have the expressions:
Sample_four_space
Sample_two_space
Sample_one_space
I would like to transform this column into:
Sample_four_space
Sample_two_space
Sample_one_space
I have tried this expression:
UPDATE My_Table
SET name = REPLACE(name,'% ',' %')
However, I would like a more robust query that would work for any length of trailing spaces. Can you help me develop a query that will remove all trailing spaces and add them to the beginning of the expression?
If you know all spaces are at the end (as in your example, then you can count them and put them at the beginning:
select concat(space(length(name) - length(replace(name, ' ', ''))),
replace(name, ' ', '')
)
Otherwise the better solution is:
select concat(space( length(name) - length(trim(trailing ' ' from name)) ),
trim(trailing ' ' from name)
)
or:
select concat(space( length(name) - length(rtrim(name)) ),
rtrim(name)
)
Both these cases count the number of spaces (in or at the end of). The space() function then replicates the spaces and concat() puts them at the beginning.
I have spent several hours to solve this problem, but I can't solve it.
One of column contain value like bellow, and the value contain some blank spaces.
光学传感器 - 光电二极管
I want select all the value equal to "光学传感器 - 光电二极管". But the blank space may be ASCII encoding('\x0a'), or utf8 encoding('\xa0'), So when I execute SQL string LIKE this, will return None.
SELECT *
FROM `icbase_icattrvalue`
WHERE `value` LIKE '光学传感器 - 光电二极管'
I have try SQL like this:
SELECT *
FROM `icbase_icattrvalue`
WHERE REPLACE( `value` , '\xa0', ' ' ) LIKE REPLACE( '光学传感器 - 光电二极管', '\xa0', ' ' )
LIMIT 0 , 30
or
SELECT *
FROM `icbase_icattrvalue`
WHERE REPLACE(`value`, CONVERT(char(160) USING utf8), ' ')
LIKE REPLACE('光学传感器 - 电二极管', CONVERT(char(160) USING utf8), ' ')
LIMIT 0 , 30
or some similar SQL string, But they can't help, they all return None.
What should I to , to select value equal to a string, but ignore such space encoding issue.
I'm sorry for my poor English.
As Alexander Gelbukh says in comment i also think that this is due to the incorrect spacing in ur code..
SELECT *
FROM `icbase_icattrvalue`
WHERE REPLACE( `value` , '\xa0', ' ' ) LIKE REPLACE( '光学传感器 - 光电二极管', '\xa0', ' ' )
LIMIT 0 , 30
Actually this is the right answer, I should replace c2a0, not a0.
NON BREAK SPACE in utf-8 is c2a0
And in python unicode is a0
I get confused with it, Thanks everybody.
SELECT * FROM `icbase_icattrvalue`
WHERE id = 197193 and
REPLACE( `value` , UNHEX('c2a0'), ' ' ) = REPLACE( '光学传感器 - 光电二极管', UNHEX('c2a0'), ' ' );
Excuse me for bad English. I have a table with a field called Subject . The value of this field is any kind of string. is there a way to find how many space char in per field . for example for "mme mme" is one and for "mme" is zero and for "mme mme mme" is two.
From MySQL List:
select length(Subject) - length(replace(Subject, ' ', ''))
Or this:
SELECT CHAR_LENGTH(Subject) - CHAR_LENGTH(REPLACE(Subject, ' ', '')) as
num_spaces FROM my_table;
If you want to do it with PHP, you can use substr_count.
substr_count(STRING_HERE, " ");
Docs to substr_count(); (PHP.NET)
It will return the number of spaces as you needed.
You can try this:
SELECT length(Subject) - length(replace(Subject, ' ', '')) FROM table;