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
 Transact-SQL (2000)
 Tough question

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-12-04 : 15:12:16
Jason writes "Field:
filename, text, 255 characters max

example filenames:
IAR_9804RT1234_description.doc
IAR_9903RT4312_description2.doc
IAR_0007CT4321_description3.doc
IAR_0212RT3321_description4.doc
IAR_YYMMXXXXXX_XXXXXXXXXXXX.doc

where YY and MM are years and months

In an sql query, I wish to sort these by their years, either ascending or descending

You can't use ORDER BY filename ASC; because '00' is considered smaller than '98'. (The computer doesn't know that '00' means 2000 and '98' means 1998)."

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2001-12-04 : 15:30:16
ORDER BY (5+SUBSTRING(filename,5,1))%10, filename
sorry, I'm in a strange sort of mood.


Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2001-12-04 : 15:44:55
This should work too:

ORDER BY Convert(datetime, SubString(FileName, 5, 4) + '01', 12),
Right(FileName, Len(FileName)-9)


Go to Top of Page

jkealey
Starting Member

6 Posts

Posted - 2001-12-04 : 15:50:57
quote:

ORDER BY (5+SUBSTRING(filename,5,1))%10, filename
sorry, I'm in a strange sort of mood.



this would limit the number of years too much, no?


Edited by - jkealey on 12/04/2001 15:52:27
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2001-12-04 : 15:57:20
Limit too much to what? You didn't say when you actually wanted it to change, so I wildly assumed 1950-2049. Seemed pretty reasonable to me.


Go to Top of Page
   

- Advertisement -