SQL - Don't let this DateTime bug catch you out!

There is a long standing bug that affects comparative operators against SQL Server datetime fields. Occasionally, it still catches out some app builders and it is something that we should be aware of. In this post, we'll walk through an example problem scenario.

Demonstration of bug

Our example app is based on a SQL server table of issue records. Our requirement is to retrieve all records with a CreateDateTime less than 2nd Jan 2020.

Just to clarify our table schema, the CreateDateTime column is of data type DateTime.
 

In theory, we can accomplish this easily by calling the Filter function, and collecting the results in a collection. This is the syntax we would use.

ClearCollect(colIssues, 
Filter('[dbo].[Issue]', CreateDateTime < DateValue("2020-01-02"))
)

However when we run this formula, it fails with this obscure error:

“Service call was successful. However, there was a problem processing the server response. Please refresh your data source and re-publish the app”.
 

What is the reason for this error?

Diagnosing this Error

In practice, the first logical step we would take is diagnose this is to follow the instructions in the error message and to refresh our data source. If we do this, we'll find that it makes no difference. Republishing the app also isn’t an option because we’re currently working in the editor.
Therefore, the next step might be to monitor our function call. At first glance, this doesn’t seem very useful because all the entries report a success.
 
 
But if we drill into the response of the getRows operation, we can see an error message that reports the following:

Conversion failed when converting date and/or time from character string


Given that our CreateDateTime column is a DateTime column, why do we receive this message, given that our formula contains no string to date time conversion?

The cause of this error and how to fix it

We can actually trace this error back to a bug that affects the SQL Datetime equality operators. This is something that I mentioned here a few years ago.
https://powerusers.microsoft.com/t5/Building-Power-Apps/Filter-not-returning-all-results-from-SQL-Database/m-p/186042

The fix for this problem is to convert the data type of the CreateDateTime column to DateTimeOffset. Not only will it prevent this type of problem, it can also mitigate problems that are related to date fields showing incorrect values, particularly when the clocks change due to daylight savings time.

If it’s not possible to change the data type of the table, an alternative is to create a view that’s based on the table, and to cast the data type of the DateTime column to DateTimeOffset.

Conclusion

There is a bug that prevents us from filtering SQL Datetime fields with equality operators. It's important to be aware of this because diagnosising this error in a typical way can cause more confusion.
With SQL Server, the best practice is to use the DateTimeOffset data type where possible.
 

Related posts

Data - How to remove trailing comma all rows in a table
February 20, 2025
Data - How to find the common rows from 3 or more collections
October 06, 2024
Data - How to show the distinct rows from 2 data sources or collections
February 26, 2024
Data - How to implement circular rotational date sorting
February 21, 2024
Bug - What to do when the data section of the Power Apps Maker portal doesn't work
June 18, 2023
Data - Combine columns from separate tables into a single table
October 13, 2022
Formula - Transposing/converting rows to columns- an almost impossible task?
September 23, 2021
Data - How to rename field names in a record
July 14, 2021
Data - How to hide duplicate rows in a gallery / show distinct multiple columns in a gallery
July 09, 2021
Data - Retrieving news/forum/blog articles with RSS
June 26, 2021
Data - How to sort by partial numbers in a text field
June 23, 2021
Data - How to return the last record from a table
June 19, 2021
Data - How to create bulk test/dummy records with random values
June 18, 2021
Data - 3 things you should know before using the MySQL or PostgreSQL connectors
May 11, 2021
Data - A walkthrough of how to migrate the data source of an app from Excel to Sharepoint
April 26, 2021
Data - How to enforce unique values (or prevent duplicate values) in one or more columns
April 19, 2021
Data - How much mobile data does Power Apps consume? What ways can we minimise this?
March 28, 2021
Data - How to save and retrieve Google calendar entries
March 14, 2021
Data - How to save and retrieve Google contacts
March 10, 2021
SQL - Caution! This is how users can hack shared SQL connections
January 23, 2021
SharePoint – 2 Mistakes to avoid when importing Excel data
January 10, 2021
Settings - What's the purpose of the "Explicit Column Selection" Setting?
January 04, 2021
SQL Server for Beginners Part 3 - Installing On-Premises Gateway
January 24, 2019
SQL Server for Beginners Part 2 - Installing Management Studio
January 14, 2019
SQL Server for Beginners Part 1 - Installing SQL Server
January 04, 2019
Searching data–What you need to know about case sensitivity
December 27, 2018
Images - How to create images that can change depending on data
November 09, 2018
Excel - Reasons NOT to use Excel as a data source
September 25, 2018
SharePoint - What you need to know about Filtering Data
September 16, 2018
Formulas - Generating Row Numbers
April 05, 2018