I have a varchar column that I have datetime values and now string values. I need to order by the datetime value and list all the string values at the bottom. I have the following for the datetime:
ORDER BY CAST(q2.[Due Date] AS DATETIME)
But my results have the value “On Hold” throughout my results. I need the “On Hold” to be on the bottom of the list.
If the only non-date value is ‘On Hold’, then this should work in most SQL engines:
I would recommend, though, that if you are storing dates as strings, then use the format ‘YYYY-MM-DD HH:MI:SS’. This will allow you to sort them without having to do a conversion.
If they are in this format, you could use something like:
(Just ordering by [Due Date] would work in most cases, since numbers sort before letters. However, an arbitrary string could start with other non-alpha characters.)