Blog
SQL - How to configure case sensitive or case insensitve searches against SQL Server data sources
When we build search screens or filter data against a SQL Server data source, we may want to specify whether Power Apps carries out a case sensitive, or case insensitive search. This post describes the behaviour of case sensitivity when we search data from Power Apps.
What is a collation sequence?
In SQL Server, a collation defines the sorting rules, case, and accent sensitivity properties for data. The collation influences how SQL Server stores data, and how it matches and sorts output when we query the database.
When we search or filter data from Power Apps using delegable functions and operators, Power Apps delegates the query to SQL Server for execution. SQL Server executes the query based on the collation settings of the data, and this is the reason why the collation matters.
We can apply a default collation at the server and database levels, and we can also apply different collations at the table and column level.
An example of a collation is shown beneath. This is is the default collation of a SQL Azure database:
SQL_Latin1_General_CP1_CI_AS
- CP1 - this specifies the 'code page' number - the code page defines the characters that we can store
- CI - CI specifies case-insensitive, whereas CS specifies case-sensitive.
- AS - AS specifies accent-sensitive, whereas AI specifies accent-insensitive.
How to carry out case insensitive searches
To ensure that searches from Power Apps are always case insensitive, we can modify the collation of the table column to use a case insensitive collation.
In the majority cases however, particularly against exisiting production databases, this isn't a viable solution. Therefore, an effective workaround is to create a view and to specify a different collation sequence for the output.
Here's an example definition of a view that converts the output columns to a case insensitive, accent insensitive collation.
CREATE VIEW vwPropertySearch AS SELECT PropertyID,
[Address1]
COLLATE Latin1_General_CI_AI AS [
[Address2]
Address1
],
COLLATE Latin1_General_CI_AI AS [
Address2
],
[City]
COLLATE Latin1_General_CI_AI AS [City],
[Postcode]
COLLATE Latin1_General_CI_AI AS [
]
Postcode
FROM dbo.Property
From Power Apps, we can now carry out a case insensitive search against the columns in this view with the following formula:
Search(vwPropertySearch,
txtSearchInputBox.Text,
"Address1",
"Address2", "City", "Postcode"
)
How to carry out accent insensitive searches
To carry out accent insensitive searches, we can apply the exact same technique from above. This is frequent request because app builders often want to allow users to search data, and to ignore the accents or diacritics in the source data.
One reason why this problem can occur more frequently is because SQL Azure defaults to an accent sensitive collation, and it is less easy to change the collation of a database, compared to an on-premise installation of SQL Server.
Conclusion
- Categories:
- sql
- SQL - How to return data from Stored Procedures to Power Apps
- SQL - How to call SQL Server Stored procedures more easily with the help of Dataverse plugins
- Dataverse - How to connect to SQL Server in Model Driven/Portal apps through virtual tables
- Forms - How to add and edit records against SQL Server tables GUID primary keys
- SQL - What to do when edit/add/delete options missing from an app, or edit controls unavailable in forms
- SQL - Caution - the Distinct function may not return the records that you expect!
- SQL - How to lookup/display related details with SQL Server Views
- Data - How to apply customize sort sequences with SQL and Power Apps
- SQL - How to show distinct values in the most efficient way
- Dataverse - How to access data more easily through TDS
- SQL - What you need to know about bulk updating records