replace multiple space with single space in a column - mysql

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.

Related

SQL CONCAT columns and adding return after each nonempty entry

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"

substring_index skips delimiter from right

I have a table 'car_purchases' with a 'description' column. The column is a string that includes first name initial followed by full stop, space and last name.
An example of the Description column is
'Car purchased by J. Blow'
I am using 'substring_index' function to extract the letter preceding the '.' in the column string. Like so:
SELECT
Description,
SUBSTRING_INDEX(Description, '.', 1) as TrimInitial,
SUBSTRING_INDEX(
SUBSTRING_INDEX(Description, '.', 1),' ', -1) as trimmed,
length(SUBSTRING_INDEX(
SUBSTRING_INDEX(Description, '.', 1),' ', -1)) as length
from car_purchases;
I will call this query 1.
picture of the result set (Result 1) is as follows
As you can see the problem is that the 'trimmed' column in the select statement starts counting the 2nd delimiter ' ' instead of the first from the right and produces the result 'by J' instead of just 'J'. Further the length column indicates that the string length is 5 instead of 4 so WTF?
However when I perform the following select statement;
select SUBSTRING_INDEX(
SUBSTRING_INDEX('Car purchased by J. Blow', '.', 1),' ', -1); -- query 2
Result = 'J' as 'Result 2'.
As you can see from result 1 the string in column 'Description' is exactly (as far as I can tell) the same as the string from 'Result 2'. But when the substring_index is performed on the column (instead of just the string itself) the result ignores the first delimiter and selects a string from the 2nd delimiter from the right of the string.
I've racked my brains over this and have tried 'by ' and ' by' as delimiters but both options do not produce the desired result of a single character. I do not want to add further complexity to query 1 by using a trim function. I've also tried the cast function on result column 'trimmed' but still no success. I do not want to concat it either.
There is an anomaly in the 'length' column of query 1 where if I change the length function to char_length function like so:
select length(SUBSTRING_INDEX(
SUBSTRING_INDEX(Description, '.', 1),' ', -1)) as length -- result = 5
select char_length(SUBSTRING_INDEX(
SUBSTRING_INDEX(Description, '.', 1),' ', -1)) as length -- result = 4
Can anyone please explain to me why the above select statement would produce 2 different results? I think this is the reason why I am not getting my desired result.
But just to be clear my desired outcome is to get 'J' not 'by J'.
I guess I could try reverse but I dont think this is an acceptable compromise. Also I am not familiar with collation and charset principles except that I just use the defaults.
Cheers Players!!!!
CHAR_LENGTH returns length in characters, so a string with 4 2-byte characters would return 4. LENGTH however returns length in bytes, so a string with 4 2-byte characters would return 8. The discrepancy in your results (including SUBSTRING_INDEX) says that the "space" between by and J is not actually a single-byte space (ASCII 0x20) but a 2-byte character that looks like a space. To workaround this, you could try replacing all unicode characters with spaces using CONVERT and REPLACE. In this example, I have an en-space unicode character in the string between by and J. The CONVERT changes that to a ?, and the REPLACE then converts that to a space:
SELECT SUBSTRING_INDEX( SUBSTRING_INDEX("Car purchased by J. Blow", '.', 1),' ', -1)
Output:
by J
With CONVERT and REPLACE:
SELECT SUBSTRING_INDEX( SUBSTRING_INDEX(REPLACE(CONVERT("Car purchased by J. Blow" USING ASCII), '?', ' '), '.', 1),' ', -1)
Output
J
For your query, you would replace the string with your column name i.e.
SELECT SUBSTRING_INDEX( SUBSTRING_INDEX(REPLACE(CONVERT(description USING ASCII), '?', ' '), '.', 1),' ', -1)
Demo on DBFiddle

Remove trailing spaces and add them as leading spaces

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.

Adding space to a string after 3 characters starting from right in mysql

I have a requirement where at first I need to remove all space from a string, then put a space after 3 characters started from right.
I have removed the spaces but putting space after certain characters is not happening.
IE:
AX1098
AX1 098
SELECT INSERT('AX1098', 4, 0, ' ');
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_insert
To update all rows:
UPDATE YOURTABLE
SET YOURCOL = INSERT(YOURCOL, 4, 0, ' ');
If strings have different length then:
update t set F = INSERT(F,LENGTH(F)-2,0,' ');

Trouble in getting rid of spaces in MySql

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), ' ')
, ' ', ' ');