简体   繁体   中英

Finding a case sensitive record

I have a requirement to fetch case sensitive records from only one column of a table.

ie their is a column email_address from which i have to find the email address which are same but they are in different cases.

for eg:

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

from the above example given.I need a query to fetch only the records abc@gmail.com and ABC@gmail.com from the column email_address .

Personally i don't like this approach, but it seems to be the only solution i have right now, and it works with mixed cases too :

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 : u need to replace the "test" with your actual table's name

try this code

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

Assuming that your DB is not case sensitive, but you need to find values that are the same, but in a different case, this might work -

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

使用以下条件

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

试试这个,

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

Try this:

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

Two ways we can Finding a case sensitive record 1.using COLLATE Latin1_General_CS_AI 2.using 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')

Here is answer,

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))  

Can be done via changing the Collation, by default it is case insensitive.

By using collation or casting to binary, like this:

Check the same answer ask here ..

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

SQL Server check case-sensitivity?

Use A wildcard sql query operator or command.. I am not sure what it is called i forgot.. you use it by adding " % " sign both on left and right part of your WHERE variable like:

SELECT * FROM TABLE WHERE COLUMN = %@VARIABLE% 

hope this helps

JUST use "LIKE" in SQL :)

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

it will display all email address ignoring the case :)

or you can use more advance like Begin with, Ends with, Contains With

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

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