Results 1 to 7 of 7

Thread: Reference a column by ordinal position?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Question 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

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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.

    BOFH Now, BOFH Past, Information on duplicates

    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...

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Reference a column by ordinal position?

    Can you rename the fields? Those aren't good names.
    Tengo mas preguntas que contestas

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    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

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    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

  7. #7
    Fanatic Member RSINGH's Avatar
    Join Date
    May 2001
    Location
    London
    Posts
    522

    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
  •  



Click Here to Expand Forum to Full Width