Click here to Skip to main content
15,915,719 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

for my application i need to split the financial year into two.

i will give 2012-2013 as input and i want to get output into two different records
ex.;
2012
2013
Posted
Comments
OriginalGriff 16-Oct-12 6:21am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.

SQL
CREATE FUNCTION ParseValues
(@String varchar(8000), @Delimiter varchar(10) )
RETURNS @RESULTS TABLE (ID int identity(1,1), Val varchar(50))
AS
BEGIN
DECLARE @Value varchar(100)
WHILE @String is not null
BEGIN
SELECT @Value=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN LEFT(@String,PATINDEX('%'+@Delimiter+'%',@String)-1) ELSE @String END, @String=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN SUBSTRING(@String,PATINDEX('%'+@Delimiter+'%',@String)+LEN(@Delimiter),LEN(@String)) ELSE NULL END
INSERT INTO @RESULTS (Val)
SELECT @Value
END
RETURN
END


to use it..
SQL
select id,val from dbo.ParseValues('2012-2013','-')
 
Share this answer
 
v2
Comments
Aarti Meswania 16-Oct-12 6:27am    
nice answer!
vote 4+
and i have include line how to use this function
:)
Hi ..

I think we don't need any loops to split the string.we can do it simply using XQuery...

SQL
--Variable declaration
DECLARE @String As VARCHAR(MAX) 
DECLARE @XmlString AS XML 

--Set the necessary string to split
SET @String = '2012-2013';

--Convert string to XML
SET @XmlString = CAST('<i>'+REPLACE(@String,'-','</i><i>')+'</i>' AS XML);

--XQuery to split the string
SELECT x.i.value('.','VARCHAR(5)') Years FROM
@XmlString.nodes('//i') As x(i)


Happy Coding..
Damodara Naidu
 
Share this answer
 
v2
SQL
Below is Split Function in SQL


SQL
DECLARE @NextString NVARCHAR(40)
DECLARE @Pos INT
DECLARE @NextPos INT
DECLARE @String NVARCHAR(40)
DECLARE @Delimiter NVARCHAR(40)

SET @String ='2011-2012'
SET @Delimiter = '-'
SET @String = @String + @Delimiter
SET @Pos = charindex(@Delimiter,@String)

WHILE (@pos <> 0)
BEGIN
SET @NextString = substring(@String,1,@Pos - 1)
SELECT @NextString -- Show Results
SET @String = substring(@String,@pos+1,len(@String))
SET @pos = charindex(@Delimiter,@String)
END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900