Thursday, 30 August 2012

Split by Delimiter In sqlserver


DECLARE @sInputList VARCHAR(8000)='7089:38:150'
,@sDelimiter VARCHAR(8000) = ':'
CREATE TABLE #List(ID INT IDENTITY(1,1),ITEM VARCHAR(1000))
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
 BEGIN
SELECT
@sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
@sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))
IF LEN(@sItem) > 0
INSERT INTO #List SELECT @sItem
 END
IF LEN(@sInputList) > 0
   INSERT INTO #List SELECT @sInputList
------------------------------------------
SELECT * FROM #List
DROP TABLE #List

No comments:

Post a Comment

Get all non-clustered indexes

DECLARE cIX CURSOR FOR     SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID         FROM Sys.Indexes SI             ...