Sort result with 4 different columns

1

I have a table with 4 different columns, 2 of them indicating the beginning (date and time) and another 2 indicating end (also date and time). Is there a way to sort the result so that it prioritizes the most recent event? for example: If the end is more recent (date + time) it would order by the end and if the beginning was more recent (date + time) it would order by the same. Thanks

I practice:

    
asked by anonymous 27.02.2015 / 22:50

1 answer

1

I do not know how these data are, but I'll assume it's all VARCHAR for this answer. Depending on the comments I change.

I think the best approach would be with a SELECT inside another, creating two more columns DATETIME so that there is a data structure capable of performing the order correctly.

SELECT NOME, DTINI, HRINI, DTFIM, HRFIM
FROM (
    SELECT NOME, DTINI, HRINI, DTFIM, HRFIM, CONVERT(DATETIME, DTINI + ' ' + HRINI) AS DATACOMPLETA_INICIAL, CONVERT(DATETIME, DTFIM + ' ' + HRFIM) AS DATACOMPLETA_FINAL
    FROM SUATABELA
    WHERE ... -- Coloque aqui sua condição Where
) 
ORDER BY DATACOMPLETA_INICIAL DESC, DATACOMPLETA_FINAL DESC
    
28.02.2015 / 06:47