September 2, 2010 at 8:10 am
Click here for the latest Delimited Split Function.
Then, it's just:
SELECT ITEM
FROM dbo.DelimitedSplit8k(@val, ',')
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
September 2, 2010 at 3:45 pm
DECLARE @FileName VARCHAR(8000)
SET @FileName='SSC1, SSC2, SSC3'
CREATE TABLE #FileNames
(FileID INT IDENTITY(1,1),
FileNameList VARCHAR(30)
)
SET @FileName=LTRIM(RTRIM(@FileName))
--SELECT @FileName
BEGIN
DECLARE @Count int
DECLARE @Cnt int
DECLARE @Len int
set @Cnt=0
if (CHARINDEX(',',@FileName,1)!=0)
BEGIN
WHILE(@Cnt<LEN(@FileName))
BEGIN
SET @COUNT=CHARINDEX(',',@FileName,@Cnt+1)
IF (@Count=0)
SET @Cnt=@Cnt+LEN(@FileName)
ELSE
BEGIN
SET @Len=@Count-@Cnt
--SELECT SUBSTRING(@FileName,@Cnt,@Len)
INSERT INTO #FileNames (FileNameList) Values (SUBSTRING(@FileName,@Cnt,@Len))
SET @Cnt=@Count+1
END
END
--Select Reverse(Substring(Reverse(@FileName),1,CHARINDEX(',',Reverse(@FileName),1)-1))
INSERT INTO #FileNames (FileNameList) Values (Reverse(Substring(Reverse(@FileName),1,CHARINDEX(',',Reverse(@FileName),1)-1)))
END
ELSE
INSERT INTO #FileNames (FileNameList) Values (@FileName)
END
SELECT * FROM #FileNames
drop table #FileNames
September 2, 2010 at 4:56 pm
WayneS (9/2/2010)
Click here for the latest Delimited Split Function.Then, it's just:
SELECT ITEM
FROM dbo.DelimitedSplit8k(@val, ',')
Nice resource, Wayne.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
September 2, 2010 at 8:06 pm
anand_vanam (9/2/2010)
DECLARE @FileName VARCHAR(8000)SET @FileName='SSC1, SSC2, SSC3'
CREATE TABLE #FileNames
(FileID INT IDENTITY(1,1),
FileNameList VARCHAR(30)
)
SET @FileName=LTRIM(RTRIM(@FileName))
--SELECT @FileName
BEGIN
DECLARE @Count int
DECLARE @Cnt int
DECLARE @Len int
set @Cnt=0
if (CHARINDEX(',',@FileName,1)!=0)
BEGIN
WHILE(@Cnt<LEN(@FileName))
BEGIN
SET @COUNT=CHARINDEX(',',@FileName,@Cnt+1)
IF (@Count=0)
SET @Cnt=@Cnt+LEN(@FileName)
ELSE
BEGIN
SET @Len=@Count-@Cnt
--SELECT SUBSTRING(@FileName,@Cnt,@Len)
INSERT INTO #FileNames (FileNameList) Values (SUBSTRING(@FileName,@Cnt,@Len))
SET @Cnt=@Count+1
END
END
--Select Reverse(Substring(Reverse(@FileName),1,CHARINDEX(',',Reverse(@FileName),1)-1))
INSERT INTO #FileNames (FileNameList) Values (Reverse(Substring(Reverse(@FileName),1,CHARINDEX(',',Reverse(@FileName),1)-1)))
END
ELSE
INSERT INTO #FileNames (FileNameList) Values (@FileName)
END
SELECT * FROM #FileNames
drop table #FileNames
Would you care to do a performance test of this code against the function that I linked to? Let's make it a nice test... say 1 million rows of data. Stuff to be split out can be any length < 8000 characters. Commas should not be in the same position in the rows... they need to be random.
I predict that the function will blow the socks off of this code. Heck, I'd predict that while this code is running, I might have enough time to go get and consume a drink.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
September 2, 2010 at 8:07 pm
CirquedeSQLeil (9/2/2010)
WayneS (9/2/2010)
Click here for the latest Delimited Split Function.Then, it's just:
SELECT ITEM
FROM dbo.DelimitedSplit8k(@val, ',')
Nice resource, Wayne.
Thanks. I've been using it so much lately, that I've made it into a snippet.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
September 3, 2010 at 3:26 am
Thanks guys but what if I have two variables
September 3, 2010 at 6:10 am
Trybbe (9/3/2010)
Thanks guys but what if I have two variables
Just call the DelimitedSplit8K function for each one.
However, I sense that there is something else going on. Do you need to link these variables together? Can you give us the complete story on this?
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply