0
I have the following table structure.
Ticket | Data | ID
The data field is datetime, ticket, id and generator are int.
What I need is for ticket and date fields to be sorted at the same time.
Current result.
Ticket Data/Hora
532 06/06/2018
532 10/08/2017
532 11/07/2018
532 01/03/2016
Expected result.
Ticket Data/Hora
532 01/03/2016
532 10/08/2017
532 06/06/2018
532 11/07/2018
Current query below.
select
w.service_req_id Ticket,
CONVERT(VARCHAR(10), w.to_time, 103) + ' ' + CONVERT(VARCHAR(8), w.to_time, 108) [Data/Hora],
'Interna' [Ação é Pública? (Pública/Interna)],
case when u.ref_id = '1' then '454'
when u.ref_id is null then ur.ref_id else u.ref_id end as Gerador,
w.description +char(13)+char(10) Descrição,
cast(w.id as varchar(100)) as Seq
from work_report w
left join sysaid_user u on u.user_name = w.user_name
left join service_req se on se.id = w.service_req_id
left join sysaid_user ur on ur.user_name = se.responsibility
where w.service_req_id in(532)
and (w.description not in('') and w.description not in ('.'))
order by 1,6
Note that I tried order by for ticket and id, but failed.
I think it’s safer to order by 1.2
– Motta
@Motta would no longer work because he had ordered the date as a string
– Sorack
"Convert" does not convert to DATE ?!
– Motta
@Motta not, in that case is converting to varchar. Aside from that putting the column position in order by is dangerous. If you add a column before breaks the logic
– Sorack
The error starts with storing data as a string, starts to complicate queries that should be trivial.
– Motta
@Motta yes but that’s not part of the question
– Sorack