I’ve just spotted a bug in some piece of code. There is an SQL similar to this:
SELECT convert(varchar(11),COL1,106) as COL1
FROM TAB
ORDER BY COL1 DESC
Now, COL1 value format returned looks like this:
17 Sep 2001
07 Mar 2011
and values stored in database look like this:
2011-03-07 00:00:00
2001-09-17 00:00:00
But because ORDER BY clause uses
17 Sep 2001
07 Mar 2011
values, ordering is incorrect. As I cannot change column names (there’s large dependency between query and application that uses it), I have to modify & fix the SQL statement to return rows with correct ordering. Is there any way to modify it in a way that ORDER BY clause uses real value stored in row’s column instead of converted one? I tried :
ORDER BY TAB.COL1 DESC
but it also didn’t work.
Thanks,Pawel
if you want sorting before converting value then try this :
or if you want sorting after converting value then try this :