Can I do this

  • Can I do this with SQL statements :-

    Input String :

    "I                                   am                      a                  boy         of              age          10."

    Output String:

    "I am a boy of age 10."

     

     

    Thanks

    Yuvraj

  • select

     replace(

      replace(

       replace(

        replace(

         'I am a boy of age 10.',

        ' ', ' '),

       ' ', ' '),

      ' ', ' '),

     ' ', ' ')

    This will only work if you can define a maximum number of spaces between words. Otherwise, I think you'll need some loooping, which is expensive.

  • The forum messed up the spaces in the post; but you get the idea.

  • If you choose looping:

    declare @s-2 varchar(1000)

    set @s-2 = 'I                                   am                      a                  boy         of              age          10.'

    while @s-2 like '%' + space(2) + '%'

     set @s-2 = replace(@S,space(2),space(1))

    select @s-2

     

  • You would put looping code like that into a user defined function, and be able to use it in a select statement returning many rows.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply