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

Use of alias in the clause ORDER BY 1

Status
Not open for further replies.

TorrediPisa

Programmer
Apr 15, 2004
67
IT
Hello

I have the following SQL statement in which I calculate the number of days between two Dates (Date1 and Date2):
I want to order the resultset by this number.
If I make:
SELECT FIELD1, FIELD2, DATE1, DATE2,
datediff('d',DATE1,DATE2) as DELAY_DAYS
ORDER BY DELAY DAYS.
I receive an error.
But if I make:
ORDER BY datediff('d',DATE1,DATE2)
It works.

Do you know if there is another way to do that?
Thanks for yr attention.
Regards

TdP

 
SELECT FIELD1, FIELD2, DATE1, DATE2,
datediff('d',DATE1,DATE2) as DELAY_DAYS
ORDER BY DELAY[red]_[/red]DAYS
 
Another way (sort by ordinal position):
SELECT FIELD1, FIELD2, DATE1, DATE2,
datediff('d',DATE1,DATE2) as DELAY_DAYS
FROM yourTable
ORDER BY 5

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thank you but I forgot to put the underscore in my message only. On the actual project it is correctly written and if I do so I receive a runtime error n. 61.

Bye
Tdp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top