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; Letters Then Numbers

Status
Not open for further replies.

mkingrey

Programmer
Apr 12, 2006
5
US
I have a recordset that I would like to sort letters to appear first then numbers to appear last. Is it possible to sort like the following?


A
B
C
R
2
3
4
7
 
How about this? It works in MSSQL.

Code:
Select Field, 

Case IsNumeric(Field)
	When 1 Then
	'1' + Field
	Else
	'0' + Field
End D
From Table
Order By D
 
And here's an ANSI/ISO compliant solution:
Code:
SELECT ...
FROM ...
WHERE ...
ORDER BY
  CASE WHEN SUBSTRING(field FROM 1 FOR 1) BETWEEN '0' AND '9' THEN 1
    ELSE 0 END,
  FIELD;
I.e. if first character is 0 - 9, place after all other rows.

(Or you can use a collation which sorts digits after letters, if available...)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top