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

ORDER BY not working correctly with datetime column

Status
Not open for further replies.

spDavCom

Programmer
Feb 16, 2003
13
US
PROBLEM:

It "ORDERS" my column by the month part of the date .

QUESTION:

How do I get it to "ORDER" on the year part of the date.

NOTES:

This stored proc is being called from an asp.net app.
 
Order it by whatever you want using the DatePart function...

DECLARE @da DateTime
SELECT @da = '10/2/1989'

SELECT
datepart(yy, @da) AS MyYear,
datepart(mm, @da) AS MyMonth,
datepart(dd, @da) AS MyDay
ORDER BY MyYear, MyMonth, MyDay
 
Also..

>> It "ORDERS" my column by the month part of the date.

This would make me guess that you are storing the date as a varchar and not as a valid DateTime datatype. From the times I have worked with datetime datatypes, they order just fine for me.
 
Thank you for your input. This is what I finally used:

ORDER BY DATEPART(YY, DATE) DESC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top