I want to ignore case when comparing string in my queries using SQL Server. So far I am able to do it using something like this:
SELECT *
FROM Venue
WHERE
Name COLLATE Latin1_general_CI_AI Like '%cafe%' COLLATE Latin1_general_CI_AI
Is there a way to set a global directive so that it would effect every query? Something like this:
SET COLLATE Latin1_general_CI_AI;
SELECT *
FROM Venue
WHERE
Name Like '%this%';
SELECT *
FROM Venue
WHERE
Name Like '%that%';
...
Thank you!
Case insensitive SQL SELECT: Use upper or lower functions or this: select * from users where lower(first_name) = 'fred'; As you can see, the pattern is to make the field you're searching into uppercase or lowercase, and then make your search string also be uppercase or lowercase to match the SQL function you've used.
The SQL keywords (SELECT, FROM, WHERE, etc.) are case-insensitive, yet they are frequently expressed in all capitals. Table and column names are case-sensitive in some settings.
Lets use Alter Command to change the column Name collation from case insensitive to case sensitive.
The way to determine if a database or database object is to check its "COLLATION" property and look for "CI" or "CS" in the result. The CI indicates that the server is case insensitive.
Is there a way to set a global directive so that it would effect every query?
No.
Collation is not a session property that applies to queries, and it cannot be changed dynamically.
The other problem with this request is that case-sensitivity is not an option that can be enabled or disabled by itself: it is a property of a collation, just like accent-sensitivity, width-sensitivity, what order the particular alphabet is arranged in, etc. A query can compare multiple fields, each with a different collation. So even if you could set a collation that would be in effect for the session, that would potentially force columns of other collations to convert collations on the fly when not even being requested to be case-insensitive. A global session setting would also affect sorting (i.e. TOP(n), ORDER BY, etc) and not just comparisons.
Since the issue is that a user wants to determine per execution whether or not to ignore part of the collation, there are a few options, but all will incur some performance penalty:
Construct the query (or queries) in Dynamic SQL:
DECLARE @SQL NVARCHAR(MAX),
@Collation NVARCHAR(50);
SET @Collation = '';
IF (@CaseInsensitive = 1)
BEGIN
SET @Collation = N'COLLATE Latin1_general_CI_AI';
END;
SET @SQL = N'SELECT *
FROM Venue
WHERE Name ' + @Collation + N' LIKE ''%' + @SearchParam
+ N'%'' ' + @Collation;
EXEC(@SQL);
Translate each character into upper-case and lower-case pairs in single-character ranges. This can be done in the app layer for the parameter value being searched on:
Force everything to the same case. Assume that the option to do case-insensitive is an additional parameter that is passed in:
SELECT *
FROM Venue
WHERE CASE @CaseInsensitive
WHEN 1 THEN LOWER(Name)
ELSE Name
END
LIKE
CASE @CaseInsensitive
WHEN 1 THEN '%' + LOWER(@SearchParam) + '%'
ELSE '%' + @SearchParam + '%'
END;
Or, do the LOWER()
prior to the query:
IF (@CaseInsensitive = 1)
BEGIN
SET @SearchParam = LOWER(@SearchParam);
END;
SELECT *
FROM Venue
WHERE CASE @CaseInsensitive
WHEN 1 THEN LOWER(Name)
ELSE Name
END
LIKE '%' + @SearchParam + '%';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With