Click here to Skip to main content
15,895,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello I would be grateful if you could help me to solve the problem. I need to write stored procedure that gets string. Each char in string have to be converted to int type and the converted type have to be inserted to the table.

Any idea haow to do it?

Thank you in advance.
Posted
Comments
Kethu Sasikanth 21-Jan-12 17:21pm    
Char. is alphabet or numeric, string ex:"ABCD" or "1203"? What DB - Orcle(pl/sql) or SQL Server(t-sql)?
Mich_90 21-Jan-12 20:24pm    
Numeric.For example "1,2,3,4"

1 solution

For MSSQL

SQL
convert(int,'01235') 



SQL
DECLARE @Pos int
SET @Pos=1;
DECLARE @inputString varchar(10)
SET @inputString='012345'

DECLARE @intValue int

while(@Pos<=LEN(@inputString))
BEGIN
	SET @intValue = CONVERT(int,substring(@inputstring,@Pos,1));
	INSERT INTO myTable(myfield) Values(@intValue);
	SET @Pos=@Pos+1
END
 
Share this answer
 
v2
Comments
Mich_90 22-Jan-12 10:21am    
virang,thank you for this solution. but what if i have a comma delimeted string?
for example:'1,2,3,4,'
virang_21 22-Jan-12 14:49pm    
Increase your pos value by 2 every iteration to take care of ",". In the while loop set your @Pos=@Pos+2
Mich_90 22-Jan-12 17:33pm    
Thank you ,virang.

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