Solved

Sql Server Trimming blank spaces

Posted on 2014-11-27
10
88 Views
Last Modified: 2014-12-22
Hi, I have a view which users connect via Excel.

Now I have a table used in the view where there is column named 'NAME' and it has data with spaces at the end
and in between like below :
Andrew  - there is single space here right at the end of the word
John - there is double space here right at the end of the word
Gareth  Jones - there is double space here in between Gareth and Jones
Thirlestane Endurance E-3120,   36m tower -- there are 3 spaces before the 36m tower

so when in my function, I need to trim these spaces in my select statement like below -
all spaces at the end of the word needs to be removed
all double and more than double spaces in between the texts should be replaced with a Single Space.

Can anyone please give me that code which I can use in my SELECT ?
Many Thanks
0
Comment
Question by:gvamsimba
  • 7
  • 2
10 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40468851
try like this :

select rtrim(replace(name, "  ","")) from table_name 

Open in new window

0
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40468870
Without functions there will always be a possible case which is not handled by a simple REPLACE() solution. This should handle the most cases:

DECLARE @Sample TABLE ( Payload NVARCHAR(255) );

INSERT  INTO @Sample
VALUES  ( 'Andrew ' ),
        ( 'John  ' ),
        ( 'Gareth  Jones' ),
        ( 'Thirlestane Endurance E-3120,   36m tower' ),
        ( ' Bob' ),
        ( 'Dr    Who' );

DECLARE @S NVARCHAR(255) = ' ';

SELECT  '[' + LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(Payload, @S + @S + @S + @S + @S + @S + @S + @S, @S), @S + @S + @S + @S, @S), @S + @S, @S), @S + @S, @S)))
        + ']'
FROM    @Sample S;

Open in new window

0
 

Author Comment

by:gvamsimba
ID: 40469039
Hi Ste5an,

I don't want to display [ ] in my result. Can you do this without that ?

Thanks
0
 

Author Comment

by:gvamsimba
ID: 40469047
Hi Haris,

Your syntax is incorrect..

Msg 1038, Level 15, State 4, Line 1
An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
0
 

Author Comment

by:gvamsimba
ID: 40469072
Hi Haris,

corrected you syntax error to select rtrim(replace(name, '  ','')) from mytable

but Gareth  Jones is now becoming GarethJones which is incorrect...it should be Gareth Jones
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:gvamsimba
ID: 40469094
Hi Ste5an,
I have now removed the [ ] from you code and got the result which I am expecting. can you please confirm if this will work in any space scenarios and just keep one space even if there are any number of spaces in between or the end ?

Thanks
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40469130
Nope, it won't.

Only using a UDF to remove the spaces can to this. You need a split function (Arrays and Lists in SQL Server). Then split the value at each spaces. Then you recreate the value with single space.
0
 

Author Comment

by:gvamsimba
ID: 40469140
Hi ste5an,

is it possible for you to give that udf which can handle my scenario so that I can call that in my current udf ?

Many thanks
0
 

Author Comment

by:gvamsimba
ID: 40513149
I achieved this by creating a function.
0
 

Author Closing Comment

by:gvamsimba
ID: 40513151
good
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Complex SQL script 1 31
powershell and sql server - alerting 7 77
Dimension table indexes 8 10
Trying to identify overlapping date ranges 5 13
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now