My Blog List

hotinit.com. Powered by Blogger.

Search This Blog

SPLIT FUNCTION Comma Seperator with RowId

Monday, 19 December 2011


CREATE FUNCTION [dbo].[fn_Split](@locationlist varchar(5000))                   
RETURNS @ParsedList Table (Rowid int,location Varchar(100))                   
AS                   
BEGIN   
 Set @locationlist = @locationlist + ','   
 DECLARE @location varchar(100), @Pos int                   
 declare @cnt int               
 SET @location =replace((LTRIM(RTRIM(@locationlist))),'''','')           
 SET @Pos = CHARINDEX(',', @locationlist, 1)                     
 Set @cnt = 1                     
 IF REPLACE(@locationlist, ',', '') <> ''                     
 BEGIN                     
  WHILE @Pos > 0                     
  BEGIN                     
   SET @location = LTRIM(RTRIM(LEFT(@locationlist, @Pos - 1)))                     
   IF @location  <> ''                     
   BEGIN                     
    INSERT INTO @ParsedList (Rowid,location)                      
    VALUES (@cnt,@location) --Use Appropriate conversion                
 Set @cnt = @cnt + 1                    
   END                     
   SET @locationlist = RIGHT(@locationlist, LEN(@locationlist) - @Pos)                     
   SET @Pos = CHARINDEX(',', @locationlist, 1)                     
                     
  END                     
 END                      
 RETURN                     
END

CharIndex

Monday, 5 December 2011


select charindex('an','madhsan')
--output
6

select charindex('an','madhsan',1)
--output(as it searches from 1st character)
6

select charindex('an','madhsan',7)
--output (as it searches from 7nth characters)
0


select patindex('%an%','madhsancoolan')
--output (Patindex is charindex plus wildcardsearch)
6
 

Blogger news

Blogroll

Most Reading

8.6/10