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)
 Sorting Text Data as Date Data

Author  Topic 

daviddeldave
Starting Member

28 Posts

Posted - 2001-12-20 : 05:08:00
Not a contradiction! I have data stored as CHAR(5) but is actually meant to be dates and because of this I am having problems getting the sorting in date order instead of text order. The data is in the for of:

APR01
MAR01
JUN01
JUL01
AUG01
SEP01
OCT01 ETC

WHEN I USE CONVERT(DATETIME,'APR01') FOR EXAMPLE I GET "Syntax error converting datetime from character string."

I think the problem is that the data relates to monthly dates as opposed to d/m/y values. Any ideas appreciated.

Thanks David

David Mercer

sica
Posting Yak Master

143 Posts

Posted - 2001-12-20 : 05:52:41
You could try a case like this:

select * from authors order by case when state = 'CA' then 1 when state = 'OR' then 2 else 3 end

You have 12 month så it can be too hard to write it...

Sica

Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2001-12-20 : 07:11:36
You'll need to give it a particular date in the month, but 3 character date names like that should be convertable. Try this:

CONVERT(datetime, '1' + monthlyDate)


Go to Top of Page

nricardo
Starting Member

17 Posts

Posted - 2001-12-20 : 11:11:51
When storing month and year data without a day, I recommend using an integer. The month and year should be stored as yyyymm. For example December 2001 would be 200112. This will always sort correctly.

Go to Top of Page
   

- Advertisement -