Posted By

pauliehaha on 06/12/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

flurk118


T-SQL Split function


 / Published in: SQL
 

  1. CREATE FUNCTION [dbo].[Split]
  2. (
  3. @pvcSearchString VARCHAR(8000),
  4. @pvcSeparator VARCHAR(5)
  5. )
  6.  
  7. RETURNS @strtable TABLE (strval VARCHAR(8000))
  8.  
  9. AS
  10.  
  11. BEGIN
  12. DECLARE
  13. @tmpStr VARCHAR(8000),
  14. @intSeparatorLength INT
  15.  
  16. SET @intSeparatorLength = LEN(@pvcSeparator)
  17.  
  18. SET @tmpStr = @pvcSearchString
  19. WHILE 1=1
  20. BEGIN
  21. INSERT INTO @strtable VALUES ( SUBSTRING(@tmpStr, 0 ,CHARINDEX(@pvcSeparator,@tmpStr)))
  22. SET @tmpStr = SUBSTRING(@tmpStr,CHARINDEX(@pvcSeparator,@tmpStr)+LEN(@pvcSeparator),8000)
  23. IF CHARINDEX(@pvcSeparator,@tmpStr) < 1
  24. BREAK
  25. END
  26.  
  27. RETURN
  28. END

Report this snippet  

You need to login to post a comment.