=============================================
-- Author: Changgyu Oh
-- Create date: 10/21/2011
-- Description: For comma separated int type items in a string, it returns
-- table having a string column and a decimal column for the items.
--
=============================================
CREATE FUNCTION [dbo].[ToStringNumericTable]
(
@ItemList varchar(500)
)
RETURNS
@ParsedList table
(
Keyword VARCHAR(100) NULL ,
KeywordNumeric decimal NULL)
AS
BEGIN
DECLARE @ItemID VARCHAR(100), @Pos int
SET @ItemList = LTRIM(RTRIM(@ItemList))+ ','
SET @Pos = CHARINDEX(',', @ItemList, 1)
IF REPLACE(@ItemList, ',', '') <> ''
BEGIN
WHILE @Pos > 0
BEGIN
SET @ItemID = LTRIM(RTRIM(LEFT(@ItemList, @Pos - 1)))
IF @ItemID <> ''
BEGIN
INSERT INTO @ParsedList (Keyword, KeywordNumeric)
VALUES( @ItemID
, CASE WHEN ISNUMERIC(@ItemID) = 1 THEN CONVERT(decimal, @ItemID)
ELSE -1 END)
END
SET @ItemList = RIGHT(@ItemList, LEN(@ItemList) - @Pos)
SET @Pos = CHARINDEX(',', @ItemList, 1)
END
END
RETURN
END
Thursday, November 17, 2011
Convert comma separated string to a table having varchar and numeric column
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment