简体   繁体   中英

SQL query to search for lowercase strings

I want a SQL query which can search for a certain word, but only in lowercase.

I am trying

select * from employees where name like LOWER('%smith%') however, this does not bring back any results.

However, when I do select * from employees where name like '%smith%' it returns employees with name SMITH and smith....in my case i just want it to return where employee names are lower-cased.

Thanks in advance!

Your default collation is likely set to a case-insensitive option. Put simply this causes all character comparison to be performed as if the sides of the comparison are of the same case, regardless of whether this is true or not.

You may use a COLLATE statement following the column name in the WHERE clause:

select * from employees where name COLLATE Latin1_General_CS_AS like '%smith%'

However, if you have a significant need for case-sensitive comparison you may wish to change the default collation of your database as explicitly marking collation is very verbose, as you can see.

Normally, in order to query a table in a case sensitive way, you would designate the column (or the entire database) with a case sensitive collation.

For performance reasons, that will be the preferred approach if you will be regularly performing case sensitive queries against this column.

However, in a pinch, you can specify the query collation on the fly. This comes at the cost of a more expensive query plan, so regard it as a last resort.

For example:

SELECT * FROM Employees
WHERE EmployeeName COLLATE SQL_Latin1_General_CP1_CS_AS 
LIKE '%smith%'

There are a variety of case sensitive collations, so you will typically want to choose the one that is closest to the standard case insensitive collation you are using.

You can check that by running these statements to check the configured collation:

--Server level
SELECT SERVERPROPERTY('COLLATION')

--Database level
SELECT DATABASEPROPERTYEX('DatabaseName', 'Collation')

--Column level
USE DatabaseName
GO
SELECT name, collation_name
FROM sys.columns
WHERE OBJECT_ID IN (SELECT OBJECT_ID
FROM sys.objects
WHERE type = 'U'
AND name = 'TableName')
AND name = 'ColumnName'

Most likely these will all return the same value. For example, SQL_Latin1_General_CP1_CI_AS, in which case the collation you would use for case sensitive queries would be SQL_Latin1_General_CP1_CS_AS.

Depending on the result you get, you will want to select the related case-sensitive collation from this list .

Use collation COLLATE Latin1_General_CP1_CS_AS to explicit Case Sensitive, default collation when installing SQL Server is SQL_Latin1_General_CP1_CI_AS

select * from employees where name COLLATE Latin1_General_CS_AS like '%smith%'

Where

CS : Case Sensitive
AS : Accent sensitive

You can also use Binary_checksum(lower()) function as below.

select * from employees where BINARY_CHECKSUM(EmployeeName) = BINARY_CHECKSUM(Lower(EmployeeName))

Please note... if the column 'Employeename' has null values, it lists you the nulls as well in your results.

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