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

ORDER BY question

Status
Not open for further replies.

Laeg

Programmer
Nov 29, 2004
95
IE
MSSQL

I have a field called FileName of type varchar containing info like 123.doc, 124.doc, 125.doc and when I do an ORDER BY FileName it manages to order the files correctly. How is this so seeing as the field is a varchar?

Thanks,
Naoise
 
Assuming that all the file names are 3 digit numbers then sorting alphabetically will be the same as ordering those numbers numerically. However, if you have something like 1234.doc in there then you will get problems:

Code:
123.doc
1234.doc
124.doc
125.doc

--James
 
Try this:

SELECT * FROM table
ORDER BY CAST(REPLACE(filename,'.doc','') AS integer)


(REPLACE is not ANSI/SQL, but still supported by many DBMS products since its specified by the ODBC API.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top