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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL to return a column

Status
Not open for further replies.

RyanEK

Programmer
Joined
Apr 30, 2001
Messages
323
Location
AU
Hi All,

Let's say I have the following table:

ID Name
------------
1 ryan
2 needs
3 help

Is there a way to write a sql so that I can return the 2nd column without specifying the field name? Some kind of column index maybe?

ie.

select column 2 from table


thanks!

Ryan
 
Why?
Maybe you have your reasons, but I didn't see ones :-)


Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
 
Yep valid question... why indeed? We should be on a beach somewhere getting a tan.

I need it to help create data warehouses and olap cubes.
 
as far as I know you can only use a column index in an Order By clause, not a select..

for example...
select * from <table>
order by 2

Jim
 
Hmmm yeah... I'm starting to think this is not possible...

Thanks anyway.
 
You could do something like this, but is seems to be overly complex, but it may help you..
Code:
declare @c varchar(10)
declare @sql varchar(100)

--change the code below for table name and column you want.
select @c = column_name from information_schema.columns
where table_name = 'vehcolors' and ordinal_position = 2

set @sql = 'select ' +@c + ' from vehcolors'

exec sp_sqlexec @sql
 
If it is just a matter of not displaying a field name...

Code:
SELECT     OrderID AS ' '
FROM         [Order Details]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top