Skip to content

Instantly share code, notes, and snippets.

@orangexception
Created July 25, 2012 13:46
Show Gist options
  • Save orangexception/3176277 to your computer and use it in GitHub Desktop.
Save orangexception/3176277 to your computer and use it in GitHub Desktop.
Integer List to Table
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