0

I would know if it possible (and how) replace all spaces secquence with only one.

I have following string: 'jkdhsf sdf jkdsf d fsjlk ds d jldksf dsf dòkjdkl ò'

my goal is condensing multiple spces in only one like :

'jkdhsf sdf jkdsf d fsjlk ds d jldksf dsf dòkjdkl ò'

I know that I can use nested replace like :

Select 
  replace(
    replace(
      replace(
        replace('jkdhsf     sdf jkdsf  d fsjlk ds   d jldksf dsf    dòkjdkl ò', '     ',1)
      , '    ',1)
     , '   ',1)
    , '  ',1) ...

but I don't know maximum number of spaces I have in my string.

So, I'm thinking to something like replace function that using regular expression matching each space repetition...

I use SqlServer 2008 R2.

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
Je Kresin
  • 1
  • 2
  • What is your required output? – Sandip - Frontend Developer Sep 14 '16 at 11:52
  • TSQL does not support regex patterns, and there is no quantifier support in TSQL patterns. You need a custom CLR function. – Wiktor Stribiżew Sep 14 '16 at 11:54
  • 1
    http://stackoverflow.com/questions/10432086/remove-all-spaces-from-a-string-in-sql-server – GuidoG Sep 14 '16 at 11:56
  • 2
    Possible duplicate of [Replace duplicate spaces with a single space in T-SQL](http://stackoverflow.com/questions/2455750/replace-duplicate-spaces-with-a-single-space-in-t-sql) – M O'Connell Sep 14 '16 at 12:14
  • It is a duplicate of question [Replace duplicate spaces with a single space in T-SQL](https://stackoverflow.com/questions/2455750/replace-duplicate-spaces-with-a-single-space-in-t-sql) – gofr1 Sep 14 '16 at 13:39

1 Answers1

0

Try this:

DECLARE @test varchar(1000)
SET @test = 'jkdhsf    sdf     jkdsf d     fsjlk ds d jldksf dsf dòkjdkl ò'

WHILE CHARINDEX('  ',@test  ) > 0
BEGIN
   SET @test = REPLACE(@test, '  ', ' ')
END

SELECT @test
  • And that will replace all spaces with `1`. OP needs to normalize spaces, not replace them with another value. – gofr1 Sep 14 '16 at 13:29
  • Any difference from this answer? http://stackoverflow.com/a/2455880/2893376 – gofr1 Sep 14 '16 at 13:36