Hi,
Below mentioned function will convert a given list of strings into a table format and will return a table.though its not a perfect optimized as varchar takes only (8000) chars.If solution for further improvement available will be heartily appreciated
GO
IF OBJECT_ID('testing.dbo.funGetTableFromList') IS NOT NULL
DROP FUNCTION funGetTableFromList;
GO
CREATE FUNCTION funGetTableFromList
(
@varSeprator VARCHAR(1),
@varString VARCHAR(MAX)
)
RETURNS @ResultTable TABLE
(
ResultString VARCHAR(4000)
)
AS
BEGIN
DECLARE @value VARCHAR(MAX);
DECLARE @Del char(1);
SET @Del=@varSeprator;
SET @varString=@varString+@Del;
WHILE charindex(@Del,@varString,0)<>0
BEGIN
SELECT @value=RTRIM(LTRIM(SUBSTRING(@varString,1,CHARINDEX(@Del,@varString,0)-1))),
@varString=RTRIM(LTRIM(SUBSTRING(@varString,CHARINDEX(@Del,@varString,0)+1,len(@varString))));
INSERT INTO @ResultTable(ResultString) VALUES(@value);
END
RETURN
END
Make a Call :
SELECT * from dbo.funGetTableFromList('|','one|two|Kamesh|Threee|you|me|we|group|part Oracle|DB|Mysql');
Thanks and regards
Kamesh shah
No comments:
Post a Comment