I am sorting songs in SQLite (on Android). I want to order them:
If you're allowed to create functions, this is what I'd create (taken from How to strip all non-alphabetic characters from string in SQL Server? and modified a bit):
Create Function [dbo].[RemoveNonAlphaNumericCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin
Declare @KeepValues as varchar(50)
Set @KeepValues = '%[^a-zA-Z0-9\s]%'
While PatIndex(@KeepValues, @Temp) > 0
Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')
Return @Temp
End
This would meet your #3 requirement and strip all the junk out of your string, then your query would look like this:
SELECT n
FROM songs
ORDER BY
CASE WHEN [dbo].[RemoveNonAlphaNumericCharacters](name) GLOB '[0-9]*' THEN 1
ELSE 0
END,
CASE WHEN [dbo].[RemoveNonAlphaNumericCharacters](name) GLOB '[0-9]*' THEN CAST(name AS INT)
ELSE [dbo].[RemoveNonAlphaNumericCharacters](name)
END
COLLATE NOCASE
It doesn't look pretty and might not have best performance. I'd probably do, what Stefan suggested. Parse your song names and insert trimmed ones into a separate column just for ordering (And of course have index on that column). It should be best solution.