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 |
|
DURGESH
Posting Yak Master
105 Posts |
Posted - 2011-12-15 : 00:54:31
|
| Hi all,Could you help me to create a function to remove special character from a string |
|
|
sql-programmers
Posting Yak Master
190 Posts |
Posted - 2011-12-15 : 00:58:23
|
| Hi,This may help you, create function dbo.RemoveSpecialChars (@s varchar(256)) returns varchar(256) with schemabindingbegin if @s is null return null declare @s2 varchar(256) set @s2 = '' declare @l int set @l = len(@s) declare @p int set @p = 1 while @p <= @l begin declare @c int set @c = ascii(substring(@s, @p, 1)) if @c between 48 and 57 or @c between 65 and 90 or @c between 97 and 122 set @s2 = @s2 + char(@c) set @p = @p + 1 end if len(@s2) = 0 return null return @s2 endSQL Server Programmers and Consultantshttp://www.sql-programmers.com/ |
 |
|
|
sql-programmers
Posting Yak Master
190 Posts |
Posted - 2011-12-15 : 01:15:57
|
| Another one function to remove special characters.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 itemUNION ALL SELECT '<' as itemUNION ALL SELECT '(' as itemUNION ALL SELECT ')' as itemUNION ALL SELECT '!' as itemUNION ALL SELECT '?' as itemUNION ALL SELECT '@' as itemUNION ALL SELECT '*' as itemUNION ALL SELECT '%' as itemUNION ALL SELECT '$' as item )SELECT @NEWSTRING = Replace(@NEWSTRING, ITEM, '') FROM SPECIAL_CHARACTER return @NEWSTRING ENDselect dbo.[FN_REMOVE_SPECIAL_CHARACTER] ('@s()antosh')SQL Server Programmers and Consultantshttp://www.sql-programmers.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-15 : 03:26:56
|
quote: Originally posted by sql-programmers Hi,This may help you, create function dbo.RemoveSpecialChars (@s varchar(256)) returns varchar(256) with schemabindingbegin if @s is null return null declare @s2 varchar(256) set @s2 = '' declare @l int set @l = len(@s) declare @p int set @p = 1 while @p <= @l begin declare @c int set @c = ascii(substring(@s, @p, 1)) if @c between 48 and 57 or @c between 65 and 90 or @c between 97 and 122 set @s2 = @s2 + char(@c) set @p = @p + 1 end if len(@s2) = 0 return null return @s2 endSQL Server Programmers and Consultantshttp://www.sql-programmers.com/
why not simply put the original linkhttp://www.source-code.biz/snippets/mssql/1.htm------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-15 : 03:27:29
|
quote: Originally posted by sql-programmers Another one function to remove special characters.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 itemUNION ALL SELECT '<' as itemUNION ALL SELECT '(' as itemUNION ALL SELECT ')' as itemUNION ALL SELECT '!' as itemUNION ALL SELECT '?' as itemUNION ALL SELECT '@' as itemUNION ALL SELECT '*' as itemUNION ALL SELECT '%' as itemUNION ALL SELECT '$' as item )SELECT @NEWSTRING = Replace(@NEWSTRING, ITEM, '') FROM SPECIAL_CHARACTER return @NEWSTRING ENDselect dbo.[FN_REMOVE_SPECIAL_CHARACTER] ('@s()antosh')SQL Server Programmers and Consultantshttp://www.sql-programmers.com/
fromhttp://www.codeproject.com/Tips/141186/To-remove-or-replace-multiple-special-character-fr------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|
|