Remove the string after a particular character(Making Sub string)

Remove the string after a particular character(Making Sub string)

This code may be used where we want to remove a the string after a particular character.
Example :- Suppose your column have like this -

ID Name
1 MNIT,Jaipur
2 GVSET,JAipur
3 MSJ,Bharatpur etc.

Now you want to remove the cities name from this column.
You can do this by simply using this code -



        UPDATE Table_Name SET Name= dbo.substr(Name)

Function should be look like this-

     
     CREATE FUNCTION dbo.substr(@string nVARCHAR(MAX))
     RETURNS nVARCHAR(MAX)
         BEGIN     
             DECLARE @SpaceIndex TinyInt     
             SET @SpaceIndex = CHARINDEX(',', @string)
             if @SpaceIndex>0         
                 set @string=( LTRIM(RTRIM(LEFT(@string, @SpaceIndex - 1))))
             else
                 set @string=(LTRIM(RTRIM(@string))) 
             RETURN @string
        END
    GO

Call By Using Below Function-


        UPDATE Tbl_Content SET Name= dbo.substr(Name)



Comments

Popular posts from this blog

Google to FTS Syntax Cheat Sheet

@@rowcount

Sql Index