Removing spaces from a string

  • Hello,

    I need to remove spaces,commas,full stops etc from a string in a column.

    For example if I have a column name called company name with the following value:

     - 'The, company. Name*was this'

    I would like to see 'thecompanynamewas'

    Can anybody provide any assistance?

    Thanks very much.

  • select replace(replace(replace(replace('The, company. Name*was this',',',''),' ',''),'.',''),'*','')

    Regards,
    Jay.

  • You might be able to use this and build a function out of it.  This will remove anything that is not from a-Z.

    SET NOCOUNT ON

    DECLARE @String AS VARCHAR(255)

    DECLARE @IntStr AS VARCHAR(255)

    DECLARE @Space AS VARCHAR(1)

    DECLARE @Loop   AS INTEGER

    DECLARE @i  AS INTEGER

    SET  @String = 'The, company. Name*was this'

    SET  @Loop = LEN(@String)

    SET  @i  = 1

    SET  @IntStr = ''

    WHILE @i <= @Loop

      BEGIN

     SET @Space = SUBSTRING(@String, @i, 1)

     IF ASCII(@Space) BETWEEN 65 AND 122

       BEGIN

      SET @IntStr = @IntStr + @Space

       END

     SET @i = @i + 1

    END

    SELECT @String, @IntStr

    SET NOCOUNT OFF

     



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

  • I bet you'll already find something like this here in the script section.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Thanks

     

    I will give it a go.

     

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

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