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.
| Author |
Topic |
|
kernelkurtz
Starting Member
2 Posts |
Posted - 2009-02-11 : 20:07:02
|
| Am using UNION ALL to get text columns from 2 tables. I want to sort the data alphabetically. To sort text, it is necessary to cast as varchar. This works fine with only one table, but when I use UNION ALL for two tables, I get an error. Just to experiment, I selected integer data instead, removed the cast, and it worked fine. Looks like you can't use UNION ALL, and SORT BY casted data. If that's the case, I need to find another approach. Thanks for any help.SQL Code:SELECT Entry FROM Table1 UNION ALL SELECT Entry FROM Table2 ORDER BY cast(Entry as varchar(500)) Error Message:ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator |
|
|
kernelkurtz
Starting Member
2 Posts |
Posted - 2009-02-11 : 21:28:22
|
| I solved my problem. I can't really explain my solution, so I'll just show it.The following code works.SELECT Entry FROM(SELECT cast(Entry as varchar(500)) FROM Table1UNION ALLSELECT cast(Entry as varchar(500)) FROM Table2) CombinedTable (Entry) ORDER BY Entry |
 |
|
|
|
|
|