Er. alokpandey's Blog

To remove or replace multiple special character from string using sql queries.

Posted in Uncategorized by Alok Kumar Pandey on January 5, 2011

In FN_REMOVE_SPECIAL_CHARACTER] we make use of WITH clause CTE to store the resultset of all special character in one row table.To remove unwanted special characters,we pass string input values and apply string’s replace funtion having special character match values from CTE table .

CREATE FUNCTION [FN_REMOVE_SPECIAL_CHARACTER] (
@INPUT_STRING varchar(300))
RETURNS VARCHAR(300)
AS
BEGIN

–declare @testString varchar(100),
DECLARE @NEWSTRING VARCHAR(100)
— set @teststring = ‘@san?poojari(darsh)’
SET @NEWSTRING = @INPUT_STRING ;
With SPECIAL_CHARACTER as
(
SELECT ‘>’ as item
UNION ALL
SELECT ‘<' as item
UNION ALL
SELECT '(' as item
UNION ALL
SELECT ')' as item
UNION ALL
SELECT '!' as item
UNION ALL
SELECT '?' as item
UNION ALL
SELECT '@' as item
UNION ALL
SELECT '*' as item
UNION ALL
SELECT '%' as item
UNION ALL
SELECT '$' as item
)
SELECT @NEWSTRING = Replace(@NEWSTRING, ITEM, '') FROM SPECIAL_CHARACTER
return @NEWSTRING
END
select dbo.[FN_REMOVE_SPECIAL_CHARACTER] ('@s()antosh')

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: