Thursday, February 25, 2010

Convert Text to Integer

Convert Text to Integer in sql server 2005:

declare @site as varchar(2000)
set @site = ‘123,234,34567,45678,567890,6789012,701,80,9′

declare @t1 table
(
t1 int
)

while charindex(‘,’,@site) > 0
begin
insert into @t1 select substring(@site,1,(charindex(‘,’,@site)-1))
SET @site = substring(@site,charindex(‘,’,@site)+1,len(@site))
end
insert into @t1
select @site

Select * from @t1

In htis exmle we declare t1 as a table variable it can create automatically, and disposes when we are complete this task. No need to worry about table variable 't1'.

It uses for converting stirng values like '101,23456,475639' -- this is string.
we will convert it as integers and pass this for querry for out put.
example:

select * from T_Employes where Employee_ID in (select * from t1)

t1 conatins all the employee id's. before all the ids are in single column separated by comma.

No comments:

Post a Comment