Remove set of characters between [abcd] in a variable

  • I have a SP which gets parameter @business like this

    [abcdef] Client

    [gdhdnuidd] 1st Party SW

    testbusiness

    I want to get rid of the charcters from [ and ]. Is there a SQL function which searches for the open bracket[ and closing bracket ] and remove the whole set of characters.

    Is it possible. Appreciate your help on this,.

    The end result i want should be like this

    Client

    1st Party SW

    testbusiness

    Basically i want to remove the characters inside the brackets and the bracket too..

    --Sam

  • How does this work?

    declare @test-2 table (Col1 varchar(50))

    insert into @test-2

    select '[abcdef] Client' UNION ALL

    select '[gdhdnuidd] 1st Party SW' UNION ALL

    select 'testbusiness'

    select case when charindex(']', Col1) > 0 then substring(Col1, Charindex(']', Col1)+2, 50)

    else Col1 end

    from @test-2

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • My 2 cents 🙂

    declare @table table(

    string varchar(50)

    )

    insert @table (string)

    select '[gdhdnuidd] 1st Party SW'

    union all select '[abcdef] Client'

    union all select 'testbusiness'

    select

    case

    when charindex(']',string) > 0 then right (string, (len(string)- charindex(']',string)))

    else string

    end finalstring

    from @table

Viewing 3 posts - 1 through 2 (of 2 total)

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