Avatar of gvamsimba
gvamsimbaFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

Sql Server Trimming blank spaces

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
Microsoft SQL Server 2008Microsoft SQL Server 2005

Avatar of undefined
Last Comment
gvamsimba
Avatar of Haris Dulic
Haris Dulic
Flag of Austria image

try like this :

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of gvamsimba
gvamsimba
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Hi Ste5an,

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

Thanks
Avatar of gvamsimba
gvamsimba
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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.
Avatar of gvamsimba
gvamsimba
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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
Avatar of gvamsimba
gvamsimba
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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
Avatar of ste5an
ste5an
Flag of Germany image

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.
Avatar of gvamsimba
gvamsimba
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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
Avatar of gvamsimba
gvamsimba
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I achieved this by creating a function.
Avatar of gvamsimba
gvamsimba
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

good
Microsoft SQL Server 2005
Microsoft SQL Server 2005

Microsoft SQL Server 2005 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. It includes support for managing XML data and allows a database server to be exposed over web services using Tabular Data Stream (TDS) packets encapsulated within SOAP (protocol) requests.

72K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo