Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 date ORDER BY HELP PLEASE

Author  Topic 

jennypretty
Yak Posting Veteran

96 Posts

Posted - 2005-04-05 : 15:43:02
Hello friends,
I have a table with a date field like this:
date
----
12/31/2004
12/22/2000
11/20/2005
10/19/2001
When I used ORDER BY clause, it doesn't sort by order. I sorted by month. Can you help me? I want to sort by year and month.
thanks,
Jenny.

The stupid question is the question you don't ask.
www.single123.com

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2005-04-05 : 15:50:14
I guess you have this as a char field instead of a datetime.

Order by Cast(yourfield as Datetime)

Also I agree with jeff see below!

Jim
Users <> Logic
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-04-05 : 15:50:43
Jenny, I'm pretty sure in the past we've recommended that you use the proper datatypes and not varchars to store your data. You should be using the datetime datatype to store dates, that way SQL Server will inherently know how to sort and compare your data. Then you can also use functions like DateAdd(), DateDiff(), Month(), Year(), etc on your values and manipulate the dates properly. Not to mention you are also then guaranteed that only valid values are stored in your database and not garbage or typos like 12/32/9185.

- Jeff
Go to Top of Page

jennypretty
Yak Posting Veteran

96 Posts

Posted - 2005-04-05 : 16:14:30
Well, last few months someone in this forum told me to change the datetime datatype to VARCHAR DATATYPE.
This is what he said:
CONVERT(VARCHAR(10), date, 101) ....
that's why it can't sort today. My original date datatype is datetime. I just looked back at the sql view I created using the CONVERT function above, and I already delete that function. Now, it worked.
Thanks guys,
Jenny.

The stupid question is the question you don't ask.
www.single123.com
Go to Top of Page
   

- Advertisement -