Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dividing one field into many and adding them

Status
Not open for further replies.

checkai

Programmer
Jan 17, 2003
1,629
0
0
US
Example field = 4;4;3;65;43;813;345

I have a variable ex; 3

I want 2 things, the 3rd value alone (3) and
the addition up to 3rd value (4+4+3)

Thanks,

Dustin
 
Declare @field varchar(50), @loc int
Set @field = '4;4;3;65;43;813;345'
Set @loc = 3
CREATE TABLE #temp (PkID Int Identity, Id INT NOT NULL)
declare @param VARCHAR(8000)
Set @param = REPLACE(@field, ';', ' UNION SELECT ')
Select @param
INSERT #temp EXEC ('SELECT ' + @param)
SELECT * FROM #temp Where pkid = @loc
Select Sum(ID) from #temp where pkid <=@loc
Drop table #Temp

HTH,
Vinod Kumar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top