Oracle update multiple spaces in a column with a single space - sql

I'm trying to update a column that could possibly have a single space or multiple spaces into just one single space using a plain sql statement not pl sql
I could do it through update table set column_name='' where column_name like '% %'
However, there could be some data such as abc def in that column. I do not want to disturb the pattern of that data meaning if want to do it only when the column is filled with white space and not touch columns that have any data.

I would recommend using a regular expression to do this, both to do the replacement and to do the matching:
UPDATE mytable
SET mycolumn = REGEXP_REPLACE(mycolumn, '\s{2,}', ' ')
WHERE REGEXP_LIKE(mycolumn, '\s{2,}')
This will replace two or more consecutive whitespace characters (spaces, tabs, etc.) with a single space. If you just want to replace spaces and not tabs, carriage returns, or newlines, use the following:
UPDATE mytable
SET mycolumn = REGEXP_REPLACE(mycolumn, ' {2,}', ' ')
WHERE REGEXP_LIKE(mycolumn, ' {2,}')
The reason for using {2,} is so that we don't bother replacing spaces where it need not be done.

With regular expression:
update table set column=regexp_replace(column, ' +', ' ')

Try:
update mytable
set col = ' '
where replace (col, ' ', null) is null;

I have used regexp_like to solve this
update table set column= ' '
where column in (select column from table where regexp_like('column','^\s+$')
Thanks

Try this:
update product set name = replace(name, ' ', ' ') where name like '% %';
where the second parameter of replace expression and like ('% %') contains two blank spaces.
In my case, the result was:
TENIS NIKE AIR => TENIS NIKE AIR
TENIS NIKE MAN => TENIS NIKE MAN

Related

Removing trailing spaces and whitespaces from SQL Server column

I have a column in my SQL Server database and it has white spaces from left and right site of the record. Basically it's a nvarchar(250) column.
I have tried removing white spaces completely like this:
UPDATE MyTable
SET whitespacecolumn = LTRIM(RTRIM(whitespacecolumn))
But this didn't work out at all, the whitespace is still there. What am I doing wrong here?
Check the below;
Find any special characters like char(10), char(13) etc in the field value.
Check the status of ANSI_PADDING ON. Refer this MSDN article.
I think replace is the way as you are looking to update
UPDATE MyTable SET whitespacecolumn = Replace(whitespacecolumn, ' ', '')
you can try doing select first and then prefer to update
SELECT *, Replace(whitespacecolumn, ' ', '') from MyTable
LTRIM, RTRIM will remove spaces in front and rear of column. In 2016 you can use TRIM function as below to trim special characters as well:
SELECT TRIM( '.,! ' FROM '# test .') AS Result;
Output:
# test

Any way to change specific data in a column e.g Mr to Dr which contains a name

I am trying to change the Titles of 'doctors' in a database and was just wondering was there a SQL query which I could run to change them.
The column im trying to change
What I am asking is that there is any way I can update the column to add a 'Dr' infront of the names to replace the 'Miss','Mr' etc.
I'm thinking about using a SQL Statement containing the wildcard function to update it but not sure it would change the specifics.
Thanks,
Karl
Use REPLACE option
UPDATE table_name
SET column_name = REPLACE(column_name, 'Mr.', 'Dr.')
WHERE column_name LIKE 'Mr.%'
try this
Update myTable
set name = replace(replace(name,'Miss ','Dr '),'Mr ','Dr ')
I might suggest doing:
update t
set col = stuff(col, 1, charindex(' ', col + ' '), 'Dr');
This only replaces the first word in the string. You might want to be extra careful and add a where clause.
update t
set col = stuff(col, 1, charindex(' ', col + ' '), 'Dr')
where col like 'Miss %' or col like 'Mr %' or
col like 'Mrs %' or col like 'Ms %';
The problem with replace() is that it replaces all occurrences in the string. Although the honorifics are unlikely to be in a name, you could have names like "Missy Elliott".

Removing blank spaces from the rows

I have a column as name which is having a results like.
name
ABC
XYZ
ader
fer
I want to remove the blank space before ader and it should print in the output like
ader.
How to achieve that?
Depending on your database you can use trim(), ltrim()/rtrim(), or replace():
select replace(name, ' ', '')
select trim(name, ' ')
select ltrim(rtrim(name))
You can use the LTRIM and RTRIM functions to remove trailing and leading spaces.
SELECT
RTRIM(LTRIM(name)) AS name
FROM yourTable

Postgresql - remove any whitespace from sub string

How can I remove any white space from substring of string?
For example I have this number '+370 650 12345'. I need all numbers to have this format country_code rest_of_the_number or in that example: +370 65012345. How could you achieve that with PostgreSQL?
I could use trim() function, but then it would remove all whitespace.
Assuming the column is named phone_number:
left(phone_number, strpos(phone_number, ' '))
||regexp_replace(substr(phone_number, strpos(phone_number, ' ') + 1), ' ', '', 'g')
It first takes everything up to the first space and then concatenates it with the result of replacing all spaces from the rest of the string.
If you also need to deal with other whitespace than just a space, you could use '\s' for the search value in regexp_replace()
If you are able to assume that a country code will always be present, you could try using a regular expression to capture the parts of interest. Assuming that your phone numbers are stored in a column named content in a table named numbers, you could try something like the following:
SELECT parts[1] || ' ' || parts[2] || parts[3]
FROM (
SELECT
regexp_matches(content, E'^\\s*(\\+\\d+)\\s+(\\d+)\\s+(\\d+)\\s*$') AS parts
FROM numbers
) t;
The following will work even if the country code is absent (see SQL Fiddle Demo here):
SELECT TRIM(REPLACE(REPLACE(REGEXP_REPLACE('+370 650 12345', '^((\+\d+)\s+)?(.*)$', '\1|\3'), ' ', ''), '|', ' '));
Returns: +370 65012345
SELECT TRIM(REPLACE(REPLACE(REGEXP_REPLACE('370 650 12345', '^((\+\d+)\s+)?(.*)$', '\1|\3'), ' ', ''), '|', ' '));
Returns: 37065012345
It looks for a country code (a set of numbers starting with a + sign) at the beginning, and replaces any whitespace following that code with a pipe |. It then replaces all the spaces in the resulting string with the empty string, then replaces occurrences of | with spaces! The choice of | is arbitrary, I suppose it could be any non-digit character.

REPLACE not working on sql table column

I've tried select REPLACE(' this is a user name', ' ', '') and it gives me 'thisisausername' which is supposed to be.
My problem is, when I try to use REPLACE on selecting a table column, it doesn't work!
My query:
SELECT REPLACE(UserName, ' ', '') as UserName FROM MY_TABLE
it still gives me usernames with spaces! Am I doing something stupid?
#AlexK. it's 160 for unicode(left(field, 1))
160 is Unicode NO-BREAK SPACE so that's what you need to replace:
replace(UserName, char(160), '')
You could update everything replacing char(160) with a whitespace ' ' and then just use your original query in the future (perhaps also ensuring such values cannot be entered in the future)