I have a SQL varchar column that has values like 100, 2000 and S5000. I want them ordered numerically rather than alphabetically. If there is a string character I am happy for the character to be either ignored or appear after the numeric values in the sort order.
In T-SQL I could this this with:
SELECT * FROM tbl
ORDER BY
CASE
WHEN ISNUMERIC(fld) = 1 THEN CONVERT(INT, fld)
ELSE 2147483647
END ASC,
fld ASC
I am wondering if there is a way to do this in Linq to SQL?
Otherwise I guess my alternatives are to execute the query directly, or create a calculated column with values like 00000100, 00002000,000S5000, etc.
Edit: I have found a solution although I am not sure how efficient it is.
from s in q
orderby (SqlMethods.Like(s.fld, "%[^0-9]%") ? Int32.MaxValue : Convert.ToInt32(s.fld)) ascending, s.fld ascending
select s;