SQL Server Funtion to Remove Specific Words

I have a function that removes the sizes (X-Small, Small, Medium, Large, X-Large, XX-Large, 3X-Large) from my string value. It is removing some of them but there are still that is not being removed, especially the Small and X-Small for some reason. Not sure why but I'm wondering if there is a more efficient and accurate way of removing these. Here are some of my strings, as you can see the ending is not consistent with the dashes before the sizes.

Fabric Series, Women's Short Sleeve Blue - X-Small
Fabric Series, Women's Short Sleeve Blue -Small
Fabric Series, Women's Short Sleeve Blue- Medium
Fabric Series, Women's Short Sleeve Blue-Large
Fabric Series, Women's Short Sleeve Blue-X-Small
Fabric Series, Women's Short Sleeve Blue - 3X-Large

ALTER FUNCTION [dbo].[RemoveSizes] 
(@strDescription varchar(1000))
RETURNS nvarchar(1000)
AS
BEGIN

declare @Return varchar(1000)
declare @badword varchar(1000)

set @badword = '- X-Small'
set @strDescription = CASE 

WHEN @strDescription LIKE '% ' + @Badword +' %'  THEN REPLACE(@strDescription,' ' +  @Badword + ' ',' ')
WHEN @strDescription LIKE @Badword +' %' THEN RIGHT(@strDescription, (len(@strDescription)-(len(@Badword)+1)))
WHEN @strDescription LIKE '% ' + @Badword THEN LEFT(@strDescription, (len(@strDescription)-(len(@Badword)+1)))
ELSE @strDescription END 

set @badword = '- Small'
set @strDescription = CASE 

WHEN @strDescription LIKE '% ' + @Badword +' %'  THEN REPLACE(@strDescription,' ' +  @Badword + ' ',' ')
WHEN @strDescription LIKE @Badword +' %' THEN RIGHT(@strDescription, (len(@strDescription)-(len(@Badword)+1)))
WHEN @strDescription LIKE '% ' + @Badword THEN LEFT(@strDescription, (len(@strDescription)-(len(@Badword)+1)))
ELSE @strDescription END

set @badword = '- Medium'
set @strDescription = CASE 

WHEN @strDescription LIKE '% ' + @Badword +' %'  THEN REPLACE(@strDescription,' ' +  @Badword + ' ',' ')
WHEN @strDescription LIKE @Badword +' %' THEN RIGHT(@strDescription, (len(@strDescription)-(len(@Badword)+1)))
WHEN @strDescription LIKE '% ' + @Badword THEN LEFT(@strDescription, (len(@strDescription)-(len(@Badword)+1)))
ELSE @strDescription END

set @badword = '- Large'
set @strDescription = CASE 

WHEN @strDescription LIKE '% ' + @Badword +' %'  THEN REPLACE(@strDescription,' ' +  @Badword + ' ',' ')
WHEN @strDescription LIKE @Badword +' %' THEN RIGHT(@strDescription, (len(@strDescription)-(len(@Badword)+1)))
WHEN @strDescription LIKE '% ' + @Badword THEN LEFT(@strDescription, (len(@strDescription)-(len(@Badword)+1)))
ELSE @strDescription END

set @badword = '- X-Large'
set @strDescription = CASE 

WHEN @strDescription LIKE '% ' + @Badword +' %'  THEN REPLACE(@strDescription,' ' +  @Badword + ' ',' ')
WHEN @strDescription LIKE @Badword +' %' THEN RIGHT(@strDescription, (len(@strDescription)-(len(@Badword)+1)))
WHEN @strDescription LIKE '% ' + @Badword THEN LEFT(@strDescription, (len(@strDescription)-(len(@Badword)+1)))
ELSE @strDescription END

set @badword = '- XX-Large'
set @strDescription = CASE 

Open in new window

Southern_GentlemanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
I'm really not following your code, but in general use REPLACE when you want to remove a subset of a string value.
Declare @badword CHAR(5) = 'Small'
SELECT REPLACE('abc_def_smallgoo_foo_boo_wackadoo_small_ding_a_ling', @badword, '') 

Open in new window

0
Scott PletcherSenior DBACommented:
For compatability, you probably want to leave it a scalar function for now.

For efficiency, use a single RETURN SELECT with multiple embedded REPLACEs:

CREATE FUNCTION [dbo].[RemoveSizes]
(@strDescription varchar(1000))
RETURNS nvarchar(1000)
AS
BEGIN
RETURN (
    SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@strDescription,
        '- X-Small', ''),
        '- Small', ''),
        '- Medium', ''),
        '- Large', ''),
        '- X-Large', ''),
        '- XX-Large', ''),
        '- 3X-Large', '')
)
END --FUNCTION
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Southern_GentlemanAuthor Commented:
Good job, Scott
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.