Removing blank spaces from the rows - sql

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

Related

How to remove WhiteSpaces using LTRIM and RTRIM to display name?

I'm new to sql and working with a column name where names are listed with spaces.
Example: Alan Joe
I am using LTRIM and RTRIM to display name as 'AlanJoe'
select LTRIM(name)
Any help how to remove spaces between the names or any links I can learn from?
Thank you
use replace() function
select replace(name,' ','')
You can try with
ISNULL(LTRIM(RTRIM((FirstName,''),'') + ' ','') + LTRIM(RTRIM((LastName,''),'')
this will make FirstName LastName combination with 1 Space if FirstName Value exists else will give only Last Name.
For removing the spaces simply use REPLACE(Name,' ','')

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

SQL query to ignore space

Is there anyway to ignore spaces in a SQL query only from right and left of a string so that I can find ' alex ' (not ' i am alex jolig ') by searching for 'alex'?
I've read this question and its answer, but the solution removes all the spaces in the string.
SELECT * FROM mytable
WHERE REPLACE(username, ' ', '') = REPLACE("John Bob Jones", ' ', '')
By the way, I'm using Sql Server Compact Edition.
Thank you.
You can use RTRIM() to remove spaces from the right and LTRIM() to remove spaces from the left hence left and right spaces removed as follows:
SELECT * FROM mytable
WHERE LTRIM(RTRIM(username)) = LTRIM(RTRIM("John Bob Jones"))
You can use RTRIM() to remove spaces from the right of string and LTRIM() to remove spaces from the left of string.
Hence left and right spaces removed as follows:
SELECT * FROM tableName
WHERE LTRIM(RTRIM(username)) = LTRIM(RTRIM("bod alias baby"))

Oracle update multiple spaces in a column with a single space

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

How could I remove unnecessary characters in SQL

I need a query that could remove unnecessary characters (a not-so-needed trailing comma as an example) from the string stored in my database table.
So that
EMAIL_ADD
abc#gmail.com,
abc#yahoo.com,def#example.org,
abs-def#ac.uk,
would update it into something like this:
EMAIL_ADD
abc#gmail.com
abc#yahoo.com,def#example.org
abs-def#ac.uk
Using TRIM() function with TRAILING option removes a specific unwanted character from end of string , in your case being a comma present at end.
UPDATE tableName
SET EMAIL_ADD = TRIM(TRAILING ',' FROM EMAIL_ADD)
See documentation here TRIM()
If you have a specific list of characters to filter out at the start and end use trim functions:
select ltrim(ltrim(rtrim(rtrim(email_add, ','), ' '), ','), ' ')
from tableX
Here I nested ltrim and rtrim to remove leading and trailing , and .
Or using trim:
select trim(trim(both ',' from email_add))
from tableX
if you only whant to remove the last character of a string you can use
update mytable set my_column = substr(my_column ,0,len(trim(my_column)-1) where mycolumn like '%,'
It is an untested example.