Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
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, November 18, 2015 9:21 AM
Points: 17, Visits: 367
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: 2 days ago @ 11:24 AM
Points: 5,736, Visits: 9,835
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: Yesterday @ 12:32 PM
Points: 2,263, Visits: 5,485
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