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.
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 paperThanks |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-08-09 : 23:38:11
|
Use the REPLACE() FunctionStarting with thisSELECT Replace(field_name, '[', '') you can extend it toSELECT Replace(Replace(field_name, '[', ''), ']', '') |
 |
|
lieml
Starting Member
6 Posts |
Posted - 2010-08-09 : 23:47:25
|
quote: Originally posted by russell Use the REPLACE() FunctionStarting with thisSELECT Replace(field_name, '[', '') you can extend it toSELECT 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 - adminCan you please advise? Thanks |
 |
|
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 paperThanks
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 |
 |
|
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); ______________________ |
 |
|
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 |
 |
|
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 paperThanks
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 |
 |
|
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.StringFROM 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 ______________________ |
 |
|
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.StringFROM 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? Thanksdeclare @str varchar(100)set @str = 'Toms doing [test] math paper [10/08/2010 12:23 - admin]'..... |
 |
|
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('') ______________________ |
 |
|
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 |
 |
|
|
|
|
|
|