Posted By

ironcrema on 01/29/14


Tagged

sql server split


Versions (?)

Split SQL Server


 / Published in: SQL
 

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

  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  

You need to login to post a comment.