• Refer to column number in query (A XP)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Refer to column number in query (A XP)

    Author
    Topic
    #426668

    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…?

    Viewing 0 reply threads
    Author
    Replies
    • #986647

      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.

      • #986666

        Aha! I *knew* I had seen something about the column number. Thanks for the clarification.

    Viewing 0 reply threads
    Reply To: Refer to column number in query (A XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: