Solved

Split in SQL Server

Posted on 2011-02-11
3
249 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 53

Accepted Solution

by:
Huseyin KAHRAMAN 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 53

Expert Comment

by:Huseyin KAHRAMAN
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server 2012 r2 and SQL 2014 6 34
T-SQL: need to reset a declared variable 4 33
SQL Server Pivot 5 42
Creating Scalar Function 3 19
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

739 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