download dowload download dowload - 3 months ago 28x
SQL Question

select max of mixed string/int column

I have a table who returns me a value as bellow

id_unique name serie timeB timeD
155488EA-FF70-49D7-99AB-AFD4125F3435 dell 14296188 05:51 06:19
1B640883-0DB6-4255-B1ED-770B6578064C dell 14295943 04:37 04:39

I want the
of the value i have tried a sql as bellow :

select max(cast(id_unique as varchar(36))),
max(name),max(serie),max(timeB),max(timeD) from mytable group by name

i got this result

1B640883-0DB6-4255-B1ED-770B6578064C dell 14296188 05:51 06:19

But the result that i need is this one :

155488EA-FF70-49D7-99AB-AFD4125F3435 dell 14296188 05:51 06:19

This any metho to fix that ?


If these are really stored as strings, I'm guessing that you want to cast the first column as a uniqueidentifier before finding the max, otherwise they will be evaluated alphabetically.

select max(cast(MyColumn as uniqueidentifier))
from MyTable