简体   繁体   中英

Empty result set from MSSQL query on Ubuntu

I am using sqlsrv module on my Ubuntu 18.04 with PHP 7.2 to run the following sql query

SELECT REPLACE(LTRIM(REPLACE(CardNo, '0', ' ')),' ', '0') AS staff_id,
    FORMAT(PunchDatetime, 'yyyy-MM-dd') as 'date',
    FORMAT(min(PunchDatetime), 'HH:mm:ss') AS time_in,
    IIF(min(PunchDatetime) != max(PunchDatetime), FORMAT(max(PunchDatetime), 'HH:mm:ss'), NULL) AS time_out
    FROM PunchRecords
    WHERE cast('2018-11-01' as date) = cast(FORMAT(PunchDatetime, 'yyyy-MM-dd') as date)
    group by CardNo, FORMAT(PunchDatetime, 'yyyy-MM-dd')
    order by CardNo";

The same query executed on Windows returns data from the table, but returns empty result set when run in Ubuntu environment.

Any feedback on the issue will be appreciated.

The most obvious reason could be the different date format on the Ubuntu. '2018-11-01' can be interpreted either as YYYY-MM-DD or YYYY-DD-MM. Just check this:

SET LANGUAGE ENGLISH;
SELECT CAST('2014-09-13' AS DATETIME);
GO
SET LANGUAGE GERMAN;
SELECT CAST('2014-09-13' AS DATETIME);--ERROR: there's no month "13"
GO

When comparing dates, just compare the date - do not convert it to string. Simply use:

WHERE '20181101' = cast(PunchDatetime as date)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM