Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Remove set of characters between [abcd] in a variable Expand / Collapse
Posted Monday, June 28, 2010 7:13 PM


Group: General Forum Members
Last Login: Wednesday, December 30, 2015 2:11 PM
Points: 17, Visits: 368
I have a SP which gets parameter @business like this

[abcdef] Client
[gdhdnuidd] 1st Party SW

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

1st Party SW

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

Post #944345
Posted Monday, June 28, 2010 10:13 PM



Group: General Forum Members
Last Login: Yesterday @ 9:37 PM
Points: 5,911, Visits: 10,366
How does this work?
declare @test table (Col1 varchar(50))
insert into @test
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

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, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #944368
Posted Monday, June 28, 2010 10:59 PM


Group: General Forum Members
Last Login: Saturday, November 19, 2016 1:44 PM
Points: 2,271, Visits: 5,545
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'

when charindex(']',string) > 0 then right (string, (len(string)- charindex(']',string)))
else string
end finalstring
from @table

Post #944376
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse