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
 General SQL Server Forums
 New to SQL Server Programming
 sort by year order, not by month

Author  Topic 

werhardt
Constraint Violating Yak Guru

270 Posts

Posted - 2008-02-27 : 08:54:49
When I sort the effective date, it sorts it in month order with all of the years together under that month.

Is it possible to sort the effective date in year order? The way it if formated right now is 01-01-2007. Is there any type of formula that I can use?

Below is the field that I use.
CONVERT(CHAR(10),pcsp.pcsp_eff , 110)

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-02-27 : 08:56:39
I am not sure what you are asking. Can you give an example?
Also, you should never be sorting on a CONVERT expression -- that is not only slower, but it sorts dates as STRINGS, not actual datetime values. Just sort simply on the DateTime column itself and it should sort properly.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

werhardt
Constraint Violating Yak Guru

270 Posts

Posted - 2008-02-27 : 09:26:27
What my client want is this.

This how it sorts now by month,
01/01/2007
02/01/2006
03/01/2005

What my client want is it got be sorted by the year like this below.

03/01/2005
02/01/2006
01/01/2007

Thanks for the heads up on the converted date.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-27 : 09:39:50
What is the datatype of the column pcsp_eff?
If it is DATETIME, then

Order by pcsp_eff

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-02-27 : 09:45:33
As I said, don't convert to a CHAR/VARCHAR. Leave your date as is. Never convert data to "make it look right" at the database layer, leave your raw data as is for sorting, comparing, calculations, etc and then simply let your client (reporting tool, web app, Excel, etc) format your data any way it wants.

Makes sense?

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2008-02-27 : 10:09:00
SELECT CONVERT(CHAR(10),pcsp.pcsp_eff , 110) AS char_pcsp_eff
FROM pcsp
ORDER BY pcsp.pcsp_eff



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -