You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
810 B

1 year ago
  1. SET ANSI_NULLS ON
  2. GO
  3. SET QUOTED_IDENTIFIER ON
  4. GO
  5. -- =============================================
  6. -- Description: Split Strings
  7. -- =============================================
  8. CREATE OR ALTER PROCEDURE PROC_SPLITSTRING
  9. @string VARCHAR(MAX)=NULL,
  10. @seperator CHAR=NULL
  11. AS
  12. BEGIN
  13. SET NOCOUNT ON;
  14. CREATE TABLE #results
  15. (
  16. strings VARCHAR(MAX)
  17. )
  18. DECLARE @result VARCHAR(MAX)
  19. DECLARE @position INT
  20. WHILE CHARINDEX(@seperator, @string) > 0
  21. BEGIN
  22. SELECT @position = CHARINDEX(@seperator, @string)
  23. SELECT @result = SUBSTRING(@string, 1, @position-1)
  24. INSERT INTO #results SELECT LTRIM(RTRIM(@result))
  25. SELECT @string = SUBSTRING(@string, @position+1, LEN(@string) - @position)
  26. END
  27. INSERT INTO #results SELECT LTRIM(RTRIM(@string))
  28. SELECT strings as 'Value' FROM #results
  29. RETURN
  30. END
  31. GO