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!

Use a select statement to return to numberical columns into one column 2

Status
Not open for further replies.

Pheonix3077

IS-IT--Management
Mar 7, 2003
6
US
I'm trying to combine two numerical columns into one column using a select statement. I can't use the concatenate (+) because it just adds the number in the field to form one number, what I need is it to merge (append) two numerical columns into one so that it returns a result of a large number thats really two separate fields just one appended after the other. Can anyone give me a suggestion on how to do that? Thanks in advance.
 
As long as numbers are positive or equal zero:
Code:
declare @blah table ( a int, b int )
insert into @blah values (0, 3)
insert into @blah values (3, 0)
insert into @blah values (23, 3)
insert into @blah values (123, 4)
insert into @blah values (42, 42)
insert into @blah values (0, 0)

select a, b, a*power(10, len(b)) + b
from @blah

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Like this

declare @i1 int,@i2 int
select @i1 =15,@i2 =20
select @i1,@i2, convert(varchar,@i1) + convert(varchar,@i2)

basically convert to varchars and use +

“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" --
 
If you need an integer back you can also do

declare @i1 int,@i2 int
select @i1 =15,@i2 =20
select @i1,@i2, convert(int,convert(varchar,@i1) + convert(varchar,@i2)) as ConvertedValue

“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" --
 
Thanks everyone for the help, the convert worked perfectly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top