Solved

Split in SQL Server

Posted on 2011-02-11
3
246 Views
Last Modified: 2012-08-14
I have values in a variable(term) as (2004,2005,2006,....2011). I would like to split the term into individual year and store in another variable say X.

Would you please help me with this. Thanks.
0
Comment
Question by:cimscims
  • 2
3 Comments
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 34872864
use this functions, then

select dbo.splitNGet('2004,2005,2006,2011',3,',')

to get the third element...
CREATE FUNCTION [dbo].[fnSplit](    
@sInputList VARCHAR(8000), -- List of delimited items   
@sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (n int, item VARCHAR(8000))
BEGIN
DECLARE @sItem VARCHAR(8000);
DECLARE @sRow int = 1;

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 @sRow, @sItem 
		set @sRow = @sRow + 1;  
	END
	IF LEN(@sInputList) > 0 INSERT INTO @List SELECT @sRow, @sInputList -- Put the last item in
	RETURN
END;

CREATE function [dbo].[splitNGet](
@sInputList VARCHAR(8000), -- List of delimited items 
@n int,  
@sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
)RETURNS varchar(8000)
begin
	declare @item varchar(8000);

	select @item = item from dbo.fnSplit(@sInputList, @sDelimiter)
	where n=@n;

	return @item;
end;

Open in new window

0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34872897
or you can use

select * from dbo.fnSplit('2004,2005,2006,2011',',')

1      2004
2      2005
3      2006
4      2011

select dbo.splitNGet('2004,2005,2006,2011',3,',')

2006
0
 

Author Closing Comment

by:cimscims
ID: 34872965
It worked.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question