Search code examples
sql-servert-sqlbootstrap-4datatablesasp-classic

Why is t-SQL order by datetime correct in SQL SMS but not in ASP Classic output?


When querying the below select in T-SQL the output is ordered as it should, but when I use the select in Classic ASP, the order is not ordering correct:

SELECT ChangeDate, ID, DataString, Operation, Area, ChangedBy
FROM EFP_LOG
WHERE Operation = 'Created'
ORDER BY ChangeDate DESC;

The output in SQL Server Management Studio is:

2021-06-06 21:13:17.177
2021-05-31 15:46:38.603
2021-05-31 15:43:21.967
2021-05-31 15:42:02.987
2021-05-31 15:41:44.493
...

Output in Classic ASP is (output format is correct displayed : DD-MM-YYYY hh.mm.ss):

05-05-2021 21:33:54
05-05-2021 21:41:52
05-05-2021 21:43:46
05-05-2021 21:45:35
06-06-2021 21:13:17
07-04-2021 07:39:03
...

Tabel Layout:

----------------------------------------------------------------------------
| PRIMARY KEY | COLUMN NAME   | DATA TYPE     | ALLOW NULL | DEFAULT VALUE |
----------------------------------------------------------------------------
|   YES       | ID            | int           | NO         | Identity (1,1)|
|             | DataString    | VARCHAR(8000) | YES        |               |
|             | Operation     | VARCHAR(255)  | YES        |               |
|             | Area          | VARCHAR(255)  | YES        |               |
|             | ChangedBy     | VARCHAR(255)  | YES        |               |
|             | ChangeDate    | datetime      | NO         | GetDate()     |
|             | ADdisplayName | VARCHAR(255)  | YES        |               |
|             | ADMail        | VARCHAR(255)  | YES        |               |
|             | ADCompany     | VARCHAR(255)  | YES        |               |
|             | ADdepartment  | VARCHAR(255)  | YES        |               |
|             | ADtitle       | VARCHAR(255)  | YES        |               |

I have tried several approaches using FORMAT, CAST and CONVERT, but none of them is working, what could be the issue?

Update with ASP code:

<%

    Set UserCreationLogConn = Server.CreateObject("ADODB.Connection")
    UserCreationLogConn.ConnectionString="Provider=SQLOLEDB; DATA SOURCE=<SERVER>;UID=SA;PWD=<PASSWORD>;DATABASE=<DATABASE>"
    UserCreationLogConn.Open

    UserCreationLogSQL = "SELECT ChangeDate, ID, DataString, Operation, Area, ChangedBy, ADmail, ADcompany, ADdepartment, ADtitle FROM EFP_LOG WHERE Operation = 'Created' ORDER BY ChangeDate DESC;"

    Set objUserCreationLog = UserCreationLogConn.Execute(UserCreationLogSQL)

    While Not objUserCreationLog.EOF

      Response.Write objUserCreationLog("ChangeDate")

    objUserCreationLog.MoveNext

    Wend
    objUserCreationLog.Close
    Set objUserCreationLog = Nothing
 %>

Output of SQL CREATE To from SMS:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[EFP_LOG](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [DataString] [varchar](8000) NULL,
    [Operation] [varchar](255) NULL,
    [Area] [varchar](255) NULL,
    [ChangedBy] [varchar](255) NULL,
    [ChangeDate] [datetime] NOT NULL,
    [ADdisplayName] [varchar](255) NULL,
    [ADmail] [varchar](255) NULL,
    [ADcompany] [varchar](255) NULL,
    [ADdepartment] [varchar](255) NULL,
    [ADtitle] [varchar](255) NULL,
 CONSTRAINT [PK_EFP_LOG] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[EFP_LOG] ADD  CONSTRAINT [DF__EFP_LOG__ChangeD__4A0FA628]  DEFAULT (getdate()) FOR [ChangeDate]
GO

Solution

  • The issue was that using DataTables, the query, and returned datetime column was treated as a text string since it could not figure out my localization.

    The solution, after checking this page https://datatables.net/reference/option/columns.type was to add the below columnDefs to the datatable

    $('#dtBasicExample').dataTable( {
      "language": {
          "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Danish.json"
      },
      "columnDefs": [
        { "type": "date", "targets": 0 }
      ]
     
    } );