SQL Server A column that looks and behaves like a date in the contingency table cannot be sorted properly

u5rb5r59  于 2023-04-04  发布在  Perl
关注(0)|答案(1)|浏览(129)

I pulled up an Excel pivot table linked to the database. I have a column in it that has a date in the format dd.mm.yyyy. The column is recognized as a date, switching between short date, long date and time works for it. It also displays the ordering from oldest to newest, rather than A to Z. But when I want to sort this column, it doesn't sort properly. For example, when I sort it from newest date to oldest, the dates come up in the following order:

30.01.2023
25.05.2022
01.09.2022
01.09.2022
12.12.2022
12.12.2022
30.01.2023
30.01.2023
02.11.2021
05.01.2022
25.05.2022
27.06.2022
18.10.2022

which don't make sense. Do you know where is the problem? I can upload a SQL query linked to this table if it helps. Thank you for any suggestions :-)

For example, I tried to make one more column from the problem column via

CONVERT(varchar, CAST(sd.posting_date as datetime), 104) AS "Invoice date"

However, that didn't help either. This column then could not be switched to the date in Excel

vyswwuz2

vyswwuz21#

You will need to add ORDER BY to get them sorted as follows :

In order by the datatype should datetime

select CONVERT(varchar, CAST(sd.posting_date as datetime), 104) AS "Invoice date"
from mytable sd
order by convert(datetime, sd.posting_date, 104) desc

相关问题