Wednesday, March 7, 2012

how to use convert data type from int to char in select statement

when i insert data from outside
i use insert into ....select...
can i use IDENTITY ( seed ,increment ) in my statement.
for example i have table
aaaaa AAA
bbbbb BBB
ccccc CCC

can i insert into new table with data

1 aaaaa AAA
2 bbbbb BBB
3 ccccc CCC
thanks

You can use cast (or convert)

create table test
(
integerValue int
)
insert into test
select 1
union all
select 232525
union all
select 2325
union all
select 525
union all
select 99232525

select integerValue, cast(integerValue as varchar(10)) as varcharValue
from test

integerValue varcharValue

1 1

232525 232525

2325 2325

525 525

99232525 99232525

|||

Well, first, don't change the meaning of a thread...I already answered it as it was originally asked Smile


create table test
(
value char(5),
otherValue char(3)
)
insert into test
select 'aaaaa','AAA'
union all
select 'bbbbb','BBB'
union all
select 'ccccc','CCC'

You can do this:


select identity(int,1,1) as test2Id,
value,
otherValue
into test2
from test

Or if you just wanted numbers, you can do this (in 2005) to get control over the values.


select row_number() over (order by value) as test3Id,
value,
otherValue
into test3
from test
go

|||sorry, i have change my question. can you answer me.

No comments:

Post a Comment