查找区分大小写的记录

时间:2014-06-23 03:44:17

标签: sql sql-server

我需要从表的一列中获取区分大小写的记录。

即他们是一个列email_address,我必须从中找到相同的电子邮件地址,但它们的情况不同。

例如:

email_ address
---------------
abc@gmail.com
hef@gmail.com
ABC@gmail.com
hrf@gmail.com

来自上面给出的示例。我需要一个查询来仅从abc@gmail.com列中获取记录ABC@gmail.comemail_address

10 个答案:

答案 0 :(得分:1)

我个人并不喜欢这种方法,但它似乎是我现在唯一的解决方案,它也适用于混合情况:

select * from test t 
    where   
    email_address in 
    (
    select email_address from test group by email_address having count(email_address)> 1        
    )
    and left(email_address, charindex('@', email_address) -1 ) collate SQL_Latin1_General_CP1_CS_AS
        not in (select left(email_address, charindex('@', email_address) -1 ) from test 
                        where email_address collate SQL_Latin1_General_CP1_CS_AS <> t.email_address )

p / s:你需要更换&#34;测试&#34;用你的实际表名

答案 1 :(得分:1)

试试这段代码

SELECT  *
FROM    table T 
JOIN    tALBE T2 
ON      T.EMAIL             =   T2.EMAIL
AND  CONVERT(varbinary,T.EMAIL)    <>       CONVERT(varbinary,T2.EMAIL)

答案 2 :(得分:1)

假设您的数据库不区分大小写,但您需要查找相同的值,但在不同的情况下,这可能有效 -

WITH CTE AS(
SELECT
email_address 
FROM TEST
GROUP BY email_address
Having count(1) > 1
)
SELECT TEST.email_address 
FROM TEST
INNER JOIN CTE ON
TEST.email_address=CTE.email_address

SQL FIDDLE

答案 3 :(得分:1)

使用以下条件

Where [email_ address] COLLATE Latin1_General_100_CI_AI = 'abc@gmail.com'

答案 4 :(得分:1)

试试这个,

Select * from TableName where email_ address COLLATE Latin1_General_CS_AS = 'Your I/p Values'

答案 5 :(得分:1)

试试这个:

SELECT *
FROM
( VALUES ('abc'), ('ABC'), ('AbC')
) x(name)
WHERE name COLLATE Latin1_General_100_CS_AI = 'abc'

答案 6 :(得分:1)

我们可以通过两种方式查找区分大小写的记录 1.使用COLLATE Latin1_General_CS_AI 2.使用BINARY_CHECKSUM

create table EmailDetails(EID int identity(1,1),EmailAddress varchar(100))
insert into EmailDetails values('abc@gmail.com')
insert into EmailDetails values('ABC@gmail.com')
select EmailAddress from EmailDetails 
where EmailAddress COLLATE Latin1_General_CS_AI=LOWER(EmailAddress)
select EmailAddress from EmailDetails 
  where EmailAddress COLLATE Latin1_General_CS_AI='abc@gmail.com'
select EmailAddress from EmailDetails 
where BINARY_CHECKSUM(EmailAddress)=BINARY_CHECKSUM('abc@gmail.com')

答案 7 :(得分:0)

这是答案,

declare @t table(email varchar(100))

insert into @t values('abc@gmail.com'),('hef@gmail.com'),('ABC@gmail.com'),('hrf@gmail.com')

declare @searchEmail varchar(100 )= 'ABC@gmail.com'

---1st approach 
SELECT *
FROM @t
WHERE   
    email = @searchEmail COLLATE SQL_Latin1_General_CP1_CS_AS

---2nd approach to convert in binary
select * from @t where CAST(email  as varbinary(100))  =  CAST(@searchEmail as varbinary(100))  

可以通过更改排序规则来完成,默认情况下它不区分大小写。

通过使用归类或转换为二进制,如下所示:

在这里检查相同的答案..

How to do a case sensitive search in WHERE clause (I'm using SQL Server)?

SQL Server check case-sensitivity?

答案 8 :(得分:0)

使用通配符sql查询运算符或命令..我不确定它叫什么我忘了..你通过在{{1}的左右两侧添加“%”符号来使用它变量如:

WHERE

希望这会有所帮助

答案 9 :(得分:-2)

只能使用&#34; LIKE&#34;在SQL中:)

SELECT email_address FROM <tablename> WHERE email_address like 'abc@gmail.com'

它会显示忽略大小写的所有电子邮件地址:)

或者你可以使用更多的进展,如开头,结束,包含

SELECT email_address FROM <tablename> WHERE email_address like '%abc%' //contains with
SELECT email_address FROM <tablename> WHERE email_address like 'abc%' //Begin with
SELECT email_address FROM <tablename> WHERE email_address like '%abc' //Ends with