Results 1 to 3 of 3
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Refer to column number in query (A XP)

    Is it possible to refer to a column by its position in a query, rather than by its name? Something like:

    SELECT 1 FROM MyTable WHERE 2 = true ORDER BY 3

    to display the first column where the second column is true, sorted by the third column. I could have sworn that I recently saw a method for doing so, but can't find it again. Actually, I found several articles that say it *can't* be done. Am I losing it...?
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Refer to column number in query (A XP)

    You can only use relative positions in the ORDER BY clause, for example

    SELECT * FROM MyTable ORDER BY 4, 2 DESC

    This will sort ascending on the 4th column and then descending on the second column.

    But you can't use relative positions in the SELECT or WHERE clauses because it would be impossible for SQL to determine whether you mean a numeric value or a relative position.

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Refer to column number in query (A XP)

    Aha! I *knew* I had seen something about the column number. Thanks for the clarification.
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •