Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 replace text between square bracket

Author  Topic 

lieml
Starting Member

6 Posts

Posted - 2010-08-09 : 23:06:28
Hi guys,

How can I use replace statement in SQL query to replace/remove all the text inside the square bracket and the square bracket itself. I have many similar text like that with square bracket in my database, can I use regular expression or something similar?

E.g. Tom's doing math paper [10/08/2010 12:23 - admin]
---> Tom's doing math paper

Thanks

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-08-09 : 23:38:11
Use the REPLACE() Function

Starting with this
SELECT Replace(field_name, '[', '')

you can extend it to
SELECT Replace(Replace(field_name, '[', ''), ']', '')
Go to Top of Page

lieml
Starting Member

6 Posts

Posted - 2010-08-09 : 23:47:25
quote:
Originally posted by russell

Use the REPLACE() Function

Starting with this
SELECT Replace(field_name, '[', '')

you can extend it to
SELECT Replace(Replace(field_name, '[', ''), ']', '')




Thank you for your reply.
I have tried your query, but it only removes the square bracket not the text inside it.
E.g. Tom's doing math paper [10/08/2010 12:23 - admin]
---> Tom's doing math paper 10/08/2010 12:23 - admin

Can you please advise? Thanks
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-08-10 : 04:37:16
quote:
Originally posted by lieml

Hi guys,

How can I use replace statement in SQL query to replace/remove all the text inside the square bracket and the square bracket itself. I have many similar text like that with square bracket in my database, can I use regular expression or something similar?

E.g. Tom's doing math paper [10/08/2010 12:23 - admin]
---> Tom's doing math paper

Thanks



declare @str varchar(100)
select @str='Toms doing math paper [10/08/2010 12:23 - admin]'
select stuff(@str,patindex('%(%',replace(@str,'[','(')),LEN(@str),'')

I wonder what happens if there are multiple '['.Something like this

'Toms doing [blah] math paper [10/08/2010 12:23 - admin]'



Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-08-10 : 05:20:42
>>
select stuff(@str,patindex('%(%',replace(@str,'[','(')),LEN(@str),'')
<<

It can be simpler like one of followings:

SELECT STUFF(@str, PATINDEX('%[[]%', @str), LEN(@str), '');

SELECT SUBSTRING(@str, 1, CHARINDEX('[', @str) - 1);


______________________
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-08-10 : 05:29:30
quote:
Originally posted by ms65g

>>
select stuff(@str,patindex('%(%',replace(@str,'[','(')),LEN(@str),'')
<<

It can be simpler like one of followings:

SELECT STUFF(@str, PATINDEX('%[[]%', @str), LEN(@str), '');

SELECT SUBSTRING(@str, 1, CHARINDEX('[', @str) - 1);


______________________




Yeah much simpler.We can name it as ver 1.1


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

lieml
Starting Member

6 Posts

Posted - 2010-08-10 : 17:18:41
quote:
Originally posted by Idera

quote:
Originally posted by lieml

Hi guys,

How can I use replace statement in SQL query to replace/remove all the text inside the square bracket and the square bracket itself. I have many similar text like that with square bracket in my database, can I use regular expression or something similar?

E.g. Tom's doing math paper [10/08/2010 12:23 - admin]
---> Tom's doing math paper

Thanks



declare @str varchar(100)
select @str='Toms doing math paper [10/08/2010 12:23 - admin]'
select stuff(@str,patindex('%(%',replace(@str,'[','(')),LEN(@str),'')

I wonder what happens if there are multiple '['.Something like this

'Toms doing [blah] math paper [10/08/2010 12:23 - admin]'



Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH



Thank you for your help guys. I have the same question like Idera what happen if it has multiple square bracket?
e.g. 'Toms doing [blah] math paper [10/08/2010 12:23 - admin]'

Thanks
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-08-10 : 17:34:20
>> I have the same question like Idera what happen if it has multiple square bracket?<<

An easy way is first split the string then filter item at last concatenate items.
You can use a inline TVF and CROSS APPLY for splitting.
For example:

SELECT D.String
FROM table_name
CROSS APPLY (SELECT substring + '' AS String
FROM tvf_splitter(REPLACE(REPLACE(string, '[', ',['), ']', '],'), ',') D
WHERE substring NOT LIKE '[[]%'
ORDER BY D.n
FOR XML PATH('')
) D


______________________
Go to Top of Page

lieml
Starting Member

6 Posts

Posted - 2010-08-10 : 18:11:42
quote:
Originally posted by ms65g

>> I have the same question like Idera what happen if it has multiple square bracket?<<

An easy way is first split the string then filter item at last concatenate items.
You can use a inline TVF and CROSS APPLY for splitting.
For example:

SELECT D.String
FROM table_name
CROSS APPLY (SELECT substring + '' AS String
FROM tvf_splitter(REPLACE(REPLACE(string, '[', ',['), ']', '],'), ',') D
WHERE substring NOT LIKE '[[]%'
ORDER BY D.n
FOR XML PATH('')
) D


______________________




Is there any chance could you apply your method in this example? Thanks

declare @str varchar(100)
set @str = 'Toms doing [test] math paper [10/08/2010 12:23 - admin]'
.....
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-08-10 : 18:18:03
First execute these codes once:
--TVF for publishing and returning sequence numbers
CREATE FUNCTION dbo.Numbers (@N INT) RETURNS TABLE AS
RETURN (WITH RecCTE (nbr) AS
(SELECT 1
UNION ALL
SELECT nbr + 1 FROM RecCTE WHERE nbr < 100),

Nums (nbr) AS
(SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1))
FROM RecCTE AS C1
CROSS APPLY
RecCTE AS C2
CROSS APPLY
RecCTE AS C3)
SELECT nbr
FROM Nums
WHERE nbr <= @N);
GO

--Creating Number Table using TVF
SELECT n.nbr
INTO Nums
FROM dbo.Numbers(10000) AS n;
GO
--TVF for Splitting the String
CREATE FUNCTION dbo.splitter (@S VARCHAR(MAX), @D CHAR(1)) RETURNS TABLE AS
RETURN (SELECT CASE WHEN CHARINDEX(@D, @S + @D, nbr) - nbr = 0 THEN ''
ELSE SUBSTRING(@S, nbr, CHARINDEX(@D, @S + @D, nbr) - nbr)
END AS Word, nbr
FROM Nums
WHERE nbr <= LEN(@S)
AND SUBSTRING(@D + @S, nbr, 1) =@D);
GO


Second try this:
SELECT word + ''
FROM dbo.splitter (REPLACE(REPLACE('Toms doing [blah] math paper [10/08/2010 12:23 - admin]'
, '[', ',['), ']', '],'), ',') d
WHERE word NOT LIKE '[[]%'
ORDER BY nbr
FOR XML PATH('')


______________________
Go to Top of Page

lieml
Starting Member

6 Posts

Posted - 2010-08-10 : 18:25:34
quote:
Originally posted by ms65g

First execute these codes once:
--TVF for publishing and returning sequence numbers
CREATE FUNCTION dbo.Numbers (@N INT) RETURNS TABLE AS
RETURN (WITH RecCTE (nbr) AS
(SELECT 1
UNION ALL
SELECT nbr + 1 FROM RecCTE WHERE nbr < 100),

Nums (nbr) AS
(SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1))
FROM RecCTE AS C1
CROSS APPLY
RecCTE AS C2
CROSS APPLY
RecCTE AS C3)
SELECT nbr
FROM Nums
WHERE nbr <= @N);
GO

--Creating Number Table using TVF
SELECT n.nbr
INTO Nums
FROM dbo.Numbers(10000) AS n;
GO
--TVF for Splitting the String
CREATE FUNCTION dbo.splitter (@S VARCHAR(MAX), @D CHAR(1)) RETURNS TABLE AS
RETURN (SELECT CASE WHEN CHARINDEX(@D, @S + @D, nbr) - nbr = 0 THEN ''
ELSE SUBSTRING(@S, nbr, CHARINDEX(@D, @S + @D, nbr) - nbr)
END AS Word, nbr
FROM Nums
WHERE nbr <= LEN(@S)
AND SUBSTRING(@D + @S, nbr, 1) =@D);
GO


Second try this:
SELECT word + ''
FROM dbo.splitter (REPLACE(REPLACE('Toms doing [blah] math paper [10/08/2010 12:23 - admin]'
, '[', ',['), ']', '],'), ',') d
WHERE word NOT LIKE '[[]%'
ORDER BY nbr
FOR XML PATH('')


______________________




Thank you for your code. I can use that to figure out my solution. Cheers
Go to Top of Page
   

- Advertisement -