|
-
May 4th, 2007, 09:26 AM
#1
Thread Starter
Hyperactive Member
Reference a column by ordinal position?
Is there a way, in an SQL select statement, to refer to a column/field by other than its name?
e.g. in the Order by clause you can use "2,1" to refer to the 2nd & 1st columns.
The problem is that some imported data has huge column names, essentially long questions "What is your favorite color and the air speed velocity ..."
and I'd rather get at the data via something like
Select 1,2,3 From xxx
or
Select Field1,Field2 etc.
I know I can do this using a recordset in VB/VBA, but I'd like to use SQL.
Thanks, DaveBo
"The wise man doesn't know all the answers, but he knows where to find them."
VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15
-
May 4th, 2007, 09:30 AM
#2
Re: Reference a column by ordinal position?
Doesn't look like it. Sorry.
Only other alternative is to create a query to sit over the table that renames the fields to managable lengths then use the query each time.
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
May 4th, 2007, 09:56 AM
#3
Frenzied Member
Re: Reference a column by ordinal position?
Can you rename the fields? Those aren't good names.
Tengo mas preguntas que contestas
-
May 4th, 2007, 01:37 PM
#4
Re: Reference a column by ordinal position?
Not "can he" - he should. If this is a table of answers to questions, name the fields something like Q1, Q2, etc. In designing a database, one of the things to keep in mind is how the names of the fields are going to be used.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
May 4th, 2007, 06:06 PM
#5
Frenzied Member
Re: Reference a column by ordinal position?
Yes, he should, but don't know if this is under his control. Don't know about you, but I work with a lot of things that I think could be greatly improved/are just plain dumb, but I'm stuck with them for one reason or another.
Tengo mas preguntas que contestas
-
Aug 29th, 2007, 08:51 AM
#6
Thread Starter
Hyperactive Member
Re: Reference a column by ordinal position?
Got it using Dynamic SQL and COL_NAME() OBJECT_ID() functions
Declare @sql VarChar(4000) -- storage for dynamic SQL string
Declare @tbl VarChar(100) -- opt. var for table name
Select @tbl = 'tbl_Accounts'
-- Note the '2', '3', '4' below for the 2nd, 3rd & 4th columns.
Select @sql= 'Select
[' + COL_NAME(OBJECT_ID(@tbl), 2) + '],
[' + COL_NAME(OBJECT_ID(@tbl), 3) + '],
[' + COL_NAME(OBJECT_ID(@tbl), 4) + ']
From tbl_Accounts'
exec (@sql)
"The wise man doesn't know all the answers, but he knows where to find them."
VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15
-
Aug 29th, 2007, 10:22 AM
#7
Fanatic Member
Re: Reference a column by ordinal position?
What db are you using? If it is SQL Server, then there is no problem in ordering by ordinal ie ORDER BY 2,1
Last edited by RSINGH; Aug 29th, 2007 at 10:24 AM.
Reason: Ignore - I've just reread your message :blush:
The liver is bad. It must be punished.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|