Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in SQL by (20.3k points)

What is the best way to remove all spaces from a string in SQL Server 2008?

LTRIM(RTRIM('  a b ')) would remove all spaces at the right and left of the string, but I also need to remove the space in the middle.

2 Answers

0 votes
by (119k points)

You can use REPLACE() function to remove the spaces in the entire column or a string. Not only spaces, but you can also replace any character with another character. Here is the SQL query to remove all spaces from a string in SQL Server:

SELECT REPLACE(Column_Name, ' ', '')

I recommend checking out this SQL Tutorial to learn more about REPLACE() function.

You can learn in-depth about SQL statements, queries and become proficient in SQL queries by enrolling in an industry-recognized SQL training.

0 votes
by (1.5k points)

As per your question you want to remove the spaces from left,right and also from middle so we are going to use replace function that is inbuilt function in ms sql

Syntax:

Select replace(columnname,’ ‘,’’)

So query will be

Select replace(‘  a  b ’,’ ‘,’’  )

Related questions

0 votes
1 answer
+3 votes
3 answers
asked May 28, 2019 in Python by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...