Sort result with 4 different columns

Asked

Viewed 108 times

1

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

Pratico:

inserir a descrição da imagem aqui

1 answer

1


I don’t know what that data looks like, but I’m gonna assume that’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 able to perform 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
  • Are varchars, I could not test yet because I am without access to VPN company, tomorrow put my advances. Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.