Intersoft Development, Inc. Home Link
If you are currently using SQL-Server for your business applications, are you sure you are realizing its full potential? SQL 2000 and 2005 are quite powerful, and now SQL Server 2008 extends this power...
Read More...
Intersoft Development emphatically recommends CodeGear Delphi for custom software development! With the newest Embarcadero Delphi 2010 release, Delphi continues its tradition of providing industry-leading unmatched software development capabilities in this RAD programming language and IDE.

Delphi 2010 builds upon the already excellent features and capabilities of Delphi 2009 by adding touch/gesturing support to recent Delphi language enhancements for Generics, Closures / Anonymous-Methods, Unicode, and much more...
Read More...
CEO's Blog Topics Below
Recent Blog Topics:
Looking for affordable professional 2D/3D CAD Software? VariCAD is a full-featured comprehensive 2D/3D CAD package offering:
  • Windows / Linux support
  • 3D modeling with automatic export 2D.
  • Sizing of mechanical components.
  • Plus, predefined library of commnon parts.
  • Quick and intuitive 3D/2D GUI
  • Compatibility with DWG, DXF, IGES, STEP and STL...

FREE 3D CAD Software Trial and Sale - VariCAD

Free Software, Source Code, and Best-Practice Documentation Index from your
Cleveland Software, Database, Web Design, Consulting, and SQL / Delphi Experts

SQL Server - User Defined Function:
Parse / Split string of delimited Integers into Table

An incredibly useful function for splitting a string of delimited integer values into a table result.
This is very handy if you have a list of integer index or primary-key values that you want to retrieve results from a table for.
Just pass a concatenated and delmited string of integer values to this function,
and join to the results of this function.

For example, @vIDs is a string of comma-separated values, like '1,2,4,22,101' :
CREATE PROCEDURE uspDoWhatever
@vIDs VARCHAR(8000)
AS

SELECT [column list] FROM [sometable]
WHERE [intcolumnname] IN (SELECT * from dbo.udfGetDelimitedIntegerListAsTable(@vIDs, ','))

/**********************************************************************************************/
-- This function takes a string of delimited numbers and explodes them out into a table.  
-- By default, the delimiter is set to '|' if another is not specified in the second parameter.
-- will need to be changed if the delimiter on the front end is ever changed to anything else.
--
-- The function also selects any "missing" values as NULL. I.e., if the string to parse is 1,,,2
-- the function assumes each (missing) value between consecutive commas represents a NULL value.
-- The calling code can turn NULLs into whatever is needed.
--
-- There is no error testing for malformed strings that contain non-numeric values other than the
-- defined delimiter.
--
-- TEST CASES:
-- ===========
--SELECT * FROM dbo.[udfGetDelimitedIntegerListAsTable]('1', ',')
--SELECT * FROM dbo.[udfGetDelimitedIntegerListAsTable]('1,', ',')
--SELECT * FROM dbo.[udfGetDelimitedIntegerListAsTable]('1,2', ',')
--SELECT * FROM dbo.[udfGetDelimitedIntegerListAsTable]('1,2,', ',')
--SELECT * FROM dbo.[udfGetDelimitedIntegerListAsTable]('1,2,0', ',')
--SELECT * FROM dbo.[udfGetDelimitedIntegerListAsTable]('', ',')
--SELECT * FROM dbo.[udfGetDelimitedIntegerListAsTable](NULL, ',')
--SELECT * FROM dbo.[udfGetDelimitedIntegerListAsTable](',', ',')
--SELECT * FROM dbo.[udfGetDelimitedIntegerListAsTable](',,,,', ',')
--SELECT * FROM dbo.[udfGetDelimitedIntegerListAsTable]('1,2', '|') --Conversion failed when converting the varchar value '1,2' to data type int.

/**********************************************************************************************/
CREATE FUNCTION [dbo].[udfGetDelimitedIntegerListAsTable]
(
	@IntegerListString VARCHAR(8000),	-- Use VARCHAR(MAX) instead of VARCHAR(8000) for SQL Server 2005
	@delimiter CHAR(1)
)
	RETURNS @IntegersTable TABLE (IntegerFromList	INT)
AS
BEGIN

	IF @IntegerListString IS NULL
		SELECT @IntegerListString = ''

	IF LEN(@IntegerListString) > 0
	BEGIN

		DECLARE @iDelimiterPosition INT
		SELECT @iDelimiterPosition =  PATINDEX('%' + @delimiter + '%', @IntegerListString)

		WHILE @iDelimiterPosition <> 0
		BEGIN
			--If nothing between delims, save as NULL
			IF LEN(SUBSTRING(@IntegerListString, 1, @iDelimiterPosition - 1)) = 0
				INSERT INTO @IntegersTable(IntegerFromList)	VALUES (NULL)
			ELSE
			INSERT INTO @IntegersTable(IntegerFromList)
				VALUES (CONVERT(INT, SUBSTRING(@IntegerListString, 1, @iDelimiterPosition - 1)))
	
			SELECT @IntegerListString = SUBSTRING(@IntegerListString, @iDelimiterPosition + 1, LEN(@IntegerListString))
			SELECT @iDelimiterPosition = PATINDEX('%' + @delimiter + '%', @IntegerListString)
		END --While...

		--If nothing after final delims, save as NULL
		IF LEN(@IntegerListString) = 0
			INSERT INTO @IntegersTable(IntegerFromList)	VALUES (NULL)
		ELSE
			INSERT INTO @IntegersTable(IntegerFromList)
				VALUES(CONVERT(INT, @IntegerListString))

	END --IF...

	RETURN 

END --Function
 
.NET framework version: 2.0.50727.3615