简体   繁体   English

SQL 查询以搜索小写字符串

[英]SQL query to search for lowercase strings

I want a SQL query which can search for a certain word, but only in lowercase.我想要一个 SQL 查询,它可以搜索某个单词,但只能是小写字母。

I am trying我在尝试

select * from employees where name like LOWER('%smith%') however, this does not bring back any results. select * from employees where name like LOWER('%smith%')但是,这不会带来任何结果。

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.但是,当我select * from employees where name like '%smith%'时,它会返回名称为 SMITH 和 smith 的员工......

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: 您可以在WHERE子句中的列名称后面使用COLLATE语句:

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. 例如,SQL_Latin1_General_CP1_CI_AS,在这种情况下,用于区分大小写的查询的排序规则将是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 使用排序 COLLATE Latin1_General_CP1_CS_AS来区分大小写,安装SQL Server时默认排序SQL_Latin1_General_CP1_CI_ASSQL_Latin1_General_CP1_CI_AS

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

Where 哪里

CS : Case Sensitive CS :区分大小写
AS : Accent sensitive AS :口音敏感

You can also use Binary_checksum(lower()) function as below.您也可以使用 Binary_checksum(lower()) function,如下所示。

select * from employees where BINARY_CHECKSUM(EmployeeName) = BINARY_CHECKSUM(Lower(EmployeeName)) select * 来自员工 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.请注意...如果“Employeename”列具有 null 值,它也会在结果中列出空值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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