Gaurav Taneja

Great dreams... never even get out of the box. It takes an uncommon amount of guts to put your dreams on the line, to hold them up and say, "How good or how bad am I?" That's where courage comes in.

  Home  |   Contact  |   Syndication    |   Login
  82 Posts | 1 Stories | 40 Comments | 7 Trackbacks

News




Google RankGoogle PR™ - Post your Page Rank with MyGooglePageRank.com



The content on this site represents my own personal opinions and thoughts at the time of posting, and does not reflect those of my employer's in any way.

Disclaimer:- All postings in this blog is provided "AS IS" with no warranties, and confers no rights.

Archives

Post Categories

Image Galleries

Atlas

Error

OutLook

SharePointService

Usefull Site Links

Remove Special Character from string

-- =============================================
-- Author:        <Author,,Name>
-- Create date: <Create Date, ,>
-- Description:    <Description, ,>
-- =============================================
ALTER  FUNCTION [dbo].[RemoveSpChar]
(
    -- Add the parameters for the function here
    @sInput varchar(MAX)=''
)
RETURNS varchar(MAX)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @sOutput Varchar(MAX),
            @iIndex int,
            @iLength int,
            @sChar varchar(1),
            @iASCII int,
            @iLen int,
            @iRem int

    set @sInput= ltrim(rtrim(@sInput))
    set @iLength = len(@sInput)
    set @iIndex =1
    set @sOutput=''

    while @iIndex <= @iLength
    begin
        set @sChar=substring(@sInput,@iIndex,1)
        set @iASCII=ascii(@sChar)
       
        if ((@iASCII>=48 and @iASCII<=57) or (@iASCII>=65 and @iASCII<=90) or (@iASCII>=97 and @iASCII<=122) )
            set @sOutput=@sOutput+@sChar
            --return @sChar + ' - ' + convert(varchar,@iASCII)
        set @iIndex =@iIndex +1
    end
   
    if len(@sOutput)>17
        set @sOutput=substring(@sOutput,1,17)
    else if len(@sOutput)<6
    begin
        set @iLen=len(@sOutput)
        set @iRem=6-@iLen
       
        set @sOutput=substring(@sOutput + replicate('0',@iRem),1,6)

    end

    -- Return the result of the function
    RETURN @sOutput

END
posted on Tuesday, April 01, 2008 9:38 PM

Feedback

# re: Remove Special Character from XML string + sql Server 4/2/2008 10:45 AM Pat
Nice function... it doesnt compile ( sql syntax errors)

# re: Remove Special Character from XML string + sql Server 11/25/2008 10:39 AM Chris
Thanks, worked like a charm on my Sql 2005 box.

# re: Remove Special Character from XML string + sql Server 1/13/2009 1:31 AM Dave
Thanks, for the script.

how do you use the function? For example, how do you remove ! and :?

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: