The following script sorts upper case addresses alphabetically followed by address lines starting with digits and finally lower case or non-alpha characters:
Use AdventureWorks;
Go
Select
AddressLine1,
AddressLine2=isnull(AddressLine2,''),
City,
[State] = sp.StateProvinceCode,
PostalCode,
Country = cr.Name
From Person.[Address] a
Join Person.StateProvince sp
On a.StateProvinceID = sp.StateProvinceID
Join Person.CountryRegion cr
On sp.CountryRegionCode = cr.CountryRegionCode
Order by
(Case
When Ascii([AddressLine1]) between 65 and 90 then 0 -- Upper case alpha
When Ascii([AddressLine1]) between 48 and 57 then 1 -- Digits
Else 2
End), AddressLine1, City
Go
Partial results:
AddressLine1 | AddressLine2 | City | State | PostalCode | Country |
Adirondack Factory Outlet | Lake George | NY | 12845 | United States | |
Alderstr 1849 | Braunschweig | NW | 38001 | Germany | |
Alderstr 2577 | Poing | SL | 66041 | Germany | |
Alderstr 2646 | Saarlouis | SL | 66740 | Germany | |
Alderstr 27 | Offenbach | SL | 63009 | Germany |
No comments:
Post a Comment