views:

74

answers:

5

hi

I have this database:

abcDEF

ABCdef

abcdef

if I write: select * from MyTbl where A='ABCdef'

how to get: ABCdef

and how to get:

abcDEF

    ABCdef

    abcdef

Thanks in advance

forgot to write - sqlCE

+6  A: 

You can make your query case sensitive by making use of the COLLATE keyword.

SELECT A 
FROM MyTbl 
WHERE A COLLATE Latin1_General_CS_AS = 'ABCdef'
RedFilter
thank's for the help, how to do it on sqlCE ?
Gold
+1  A: 

It's all about collation. Each one has a suffix (CI and CS, meaning Case Insensitive, and Case Sensitive).

http://www.databasejournal.com/features/mssql/article.php/10894_3302341_2/SQL-Server-and-Collation.htm

Alexander
A: 

SQL is non-case-sensitive by default, so you will get all three items if doing a simple string comparison. To make it case-sensitive, you can cast the value of the field and your search value as varbinary:

SELECT * FROM MyTbl WHERE CAST(A AS varbinary(20)) = CAST('ABCdef' as varbinary(20))

The above assumes your varchar or nvarchar field is sized at 20.

Josh
-1 Whow. That is the worst answer possible - take out all indices, force a table scan and totally ignore the reality of being able to change the collation ;)
TomTom
@TomTom - Using `COLLATE` means the indices won't be used anyway.
Martin Smith
I'm sure there are worse answers -- that query at least functions. From my understanding there's no way to specify case insensitivity using indexes unless you're doing a binary comparison. I've never tried it, but if your table column was defined as a VARBINARY, you could do an index on that and only have to cast your search string.
Josh
+2  A: 

If you have abcDEF, ABCdef, abcdef already in the database then it's already case sensitive or you have no constraint.

You'd have to add a COLLATE on both sides to make sure it's truly case sensitive (for a non case sensitive database) which will invalidate index usage

SELECT TheColumn
FROM MyTable 
WHERE TheColumn COLLATE Latin1_General_CS_AS = 'ABCdef' COLLATE Latin1_General_CS_AS

What about accents too? Latin1_General_CS_AI, Latin1_General_Bin?

gbn
+1  A: 

For SQL Compact, you can make the entire database case senstive - http://erikej.blogspot.com/2008/07/working-with-case-sensitive-sql-compact.html -

To get all 3 rows do

select * from MyTbl where LOWER(A)='abcdef'
ErikEJ