March 14, 2014 at 6:34 am
CREATE TABLE #tblNoise
(
Noisy varchar(100),
)
GO
INSERT INTO #tblNoise (Noisy)
SELECT 'the' UNION ALL
SELECT 'and' UNION ALL
SELECT 'of' UNION ALL
SELECT 'or'
DECLARE @STR varchar(100) = 'the institute of comics'
I need a string that looks like 'institute comics'. I can split the string on the spaces into a table but how do I rebuild a string with the noise words missing?
March 14, 2014 at 6:37 am
Replace function might do what you are wanting.
March 14, 2014 at 7:55 am
Here is one way.
select @STR = REPLACE(@str, Noisy, '')
from #tblNoise
select replace(ltrim(rtrim(@str)), ' ', ' ') as Result
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 14, 2014 at 9:56 am
Luis Cazares (3/14/2014)
Just to keep it safer, here's a modification to Sean's option.
DECLARE @STR varchar(100) = 'the foreign institute of comics'
select @STR = REPLACE(' ' + @STR + ' ', ' ' + Noisy + ' ', ' ')
from #tblNoise
select ltrim(@str) as Result
What do you mean about this being safer? Not saying it is or it isn't, just not sure what you mean.
You also would probably want to add an rtrim so you don't get a bunch of spaces on the right.
We could shorten this up even more.
select @STR = replace(ltrim(rtrim(REPLACE(@str, Noisy, ''))), ' ', ' ')
from #tblNoise
select @STR
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 14, 2014 at 10:03 am
With out the spaces added to both sides of the words, the string 'the foreign institute of comics' would become 'feign institute comics', just to give an example.
March 14, 2014 at 10:15 am
Luis Cazares (3/14/2014)
With out the spaces added to both sides of the words, the string 'the foreign institute of comics' would become 'feign institute comics', just to give an example.
Ahh yes. Didn't think about that. I figured there was something but just wasn't seeing it. Thanks for the catch.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply