Split SQL Server


/ Published in: SQL
Save to your folder(s)

Función sql que devuelve una tabla. parametros ID, cadena, caracter_slit


Copy this code and paste it in your HTML
  1. SET ANSI_NULLS ON
  2. GO
  3. SET QUOTED_IDENTIFIER ON
  4. GO
  5. ALTER FUNCTION [dbo].[Split]
  6. (
  7. @ID INT,
  8. @RowData nvarchar(MAX),
  9. @SplitOn nvarchar(5)
  10. )
  11. RETURNS @ReturnValue TABLE
  12. (ID INT, DATA NVARCHAR(MAX))
  13. AS
  14. BEGIN
  15. DECLARE @Counter INT
  16. SET @Counter = 1
  17. While (Charindex(@SplitOn,@RowData)>0)
  18. BEGIN
  19. INSERT INTO @ReturnValue (ID, DATA)
  20. SELECT ID = @ID, DATA =
  21. ltrim(rtrim(SUBSTRING(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
  22. SET @RowData =
  23. SUBSTRING(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
  24. SET @Counter = @Counter + 1
  25. END
  26. INSERT INTO @ReturnValue (ID, DATA)
  27. SELECT ID=@ID, DATA = ltrim(rtrim(@RowData))
  28. RETURN
  29. END

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.