Solved

Split in SQL Server

Posted on 2011-02-11
3
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 56

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 56

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

617 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