Created
July 25, 2012 13:46
-
-
Save orangexception/3176277 to your computer and use it in GitHub Desktop.
Integer List to Table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALTER FUNCTION [dbo].[fn_IntegerListToTable] ( | |
@liValues varchar( 500 ) | |
) | |
RETURNS | |
@tReturn TABLE | |
( | |
iID integer | |
) | |
AS | |
BEGIN | |
DECLARE | |
@sInteger varchar( 10 ) , | |
@iPosition integer | |
SET @liValues= LTRIM( RTRIM( @liValues ) ) + ',' | |
SET @iPosition= CHARINDEX( ',' , @liValues , 1 ) | |
IF REPLACE( @liValues , ',' , '' ) <> '' | |
BEGIN | |
WHILE @iPosition > 0 | |
BEGIN | |
SET @sInteger= LTRIM( RTRIM( LEFT( @liValues , @iPosition - 1 ) ) ) | |
IF @sInteger <> '' | |
BEGIN | |
INSERT INTO @tReturn | |
SELECT CAST( @sInteger AS integer ) | |
END | |
SET @liValues= RIGHT( @liValues , LEN( @liValues ) - @iPosition ) | |
SET @iPosition= CHARINDEX( ',' , @liValues , 1 ) | |
END | |
END | |
RETURN | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment