Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 union of 2 columns in one

Author  Topic 

kote_alex
Posting Yak Master

112 Posts

Posted - 2009-06-17 : 09:02:50
I have a nvarchar column and an int column
I tried this
select distinct cast(name as int) from callsheet.dbo.product
union all
select distinct id from callsheet.dbo.product

but I can't convert it because name is nvarchar

nr
SQLTeam MVY

12543 Posts

Posted - 2009-06-17 : 09:05:23
select distinct name from callsheet.dbo.product
union all
select distinct convert(varchar(20),id) from callsheet.dbo.product

Strange values to union - maybe

select distinct name, id = convert(int,null) from callsheet.dbo.product
union all
select distinct null, id from callsheet.dbo.product


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

kote_alex
Posting Yak Master

112 Posts

Posted - 2009-06-17 : 09:50:40
so the expected result should look like this ... name + number in one cell
like this xxxxx + (1234567)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-17 : 09:52:37
select distinct cast(id as varchar(200)) + cast(name as varchar(200) from callsheet.dbo.product



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

kote_alex
Posting Yak Master

112 Posts

Posted - 2009-06-17 : 09:58:53
quote:
Originally posted by Peso

select distinct cast(id as varchar(200)) + cast(name as varchar(200) from callsheet.dbo.product



E 12°55'05.63"
N 56°04'39.26"




thanks !!! it worked ! :D
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2009-06-17 : 09:59:38
select name + ' + (' + convert(varchar(20),id) + ')' from callsheet.dbo.product

I've taken out the distinct - put it back in if it is needed but it's best to remove it if the values are unique.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -