Come splittare una stringa e trasformarla in una tabella in SQLSERVER
/*** Object: Function [dbo].[SplitString] by:ALLDONE Script Date: 19/04/2014 15:30:10 ***/
USE [prjYOURDB];
GO
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
CREATE FUNCTION [dbo].[SplitString]
(@input varchar(4000))
RETURNS @Result table
(
[valore] varchar(50) COLLATE Latin1_General_CI_AS NULL
)
WITH EXEC AS CALLER
AS
BEGIN
DECLARE @str VARCHAR(50)
DECLARE @ind Int
IF(@input is not null)
BEGIN
SET @ind = CharIndex(',',@input)
WHILE @ind > 0
BEGIN
SET @str = SUBSTRING(@input,1,@ind-1)
SET @input = SUBSTRING(@input,@ind+1,LEN(@input)-@ind)
INSERT INTO @Result values (@str)
SET @ind = CharIndex(',',@input)
END
SET @str = @input
INSERT INTO @Result values (@str)
END
RETURN
END
GO
ciao ciao