103

I'm looking for an SQL query that gives me all rows where ColumnX contains any lowercase letter (e.g. "1234aaaa5789"). Same for uppercase.

ripper234
  • 222,824
  • 274
  • 634
  • 905

15 Answers15

127
SELECT * FROM my_table 
WHERE UPPER(some_field) != some_field

This should work with funny characters like åäöøüæï. You might need to use a language-specific utf-8 collation for the table.

geon
  • 8,128
  • 3
  • 34
  • 41
  • 35
    It does not work in case insensitive DB, must add COLLATE Latin1_General_CS_AS from Devraj Gadhavi answer – Pawel Cioch Jul 02 '14 at 19:49
  • 1
    In MySQL 5.6, `Latin1_General_CS_AS` caused an error. `COLLATE latin1_general_cs` worked though. – kouton Apr 08 '16 at 06:16
  • @kouton edited [my answer](http://stackoverflow.com/a/14646445/1184435) for more details on the issue you faced. – Devraj Gadhavi Dec 21 '16 at 04:57
  • 1
    This doesn't work in MySQL without the BINARY adjective. So: SELECT * FROM my_table WHERE BINARY UPPER(some_field) != BINARY some_field – Chiwda Dec 16 '21 at 03:29
53
SELECT * FROM my_table WHERE my_column = 'my string'
COLLATE Latin1_General_CS_AS

This would make a case sensitive search.


EDIT

As stated in kouton's comment here and tormuto's comment here whosoever faces problem with the below collation

COLLATE Latin1_General_CS_AS

should first check the default collation for their SQL server, their respective database and the column in question; and pass in the default collation with the query expression. List of collations can be found here.

Community
  • 1
  • 1
Devraj Gadhavi
  • 3,541
  • 3
  • 38
  • 67
  • This didn't work from my server's phpmyadmin; i got an error: Unknown collation: 'Latin1_General_CS_AS'. So i had to use a more available collation instead 'Latin1_General_CS' and it worked. – tormuto Sep 14 '16 at 13:34
  • @tormuto edited the answer; and added some more details on the issue you faced. Though I am not sure whether it would work for MySQL, you might find something for MySQL similar to what MS SQL server offers. – Devraj Gadhavi Dec 21 '16 at 05:00
43
SELECT * FROM Yourtable 
WHERE UPPER([column_NAME]) COLLATE Latin1_General_CS_AS !=[Column_NAME]
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
NBS
  • 449
  • 4
  • 4
27

This is how I did it for utf8 encoded table and utf8_unicode_ci column, which doesn't seem to have been posted exactly:

SELECT *
FROM table
WHERE UPPER(column) != BINARY(column)
martincarlin87
  • 10,848
  • 24
  • 98
  • 145
15

for search all rows in lowercase

SELECT *
FROM Test
WHERE col1 
LIKE '%[abcdefghijklmnopqrstuvwxyz]%'
collate Latin1_General_CS_AS

Thanks Manesh Joseph

El David
  • 616
  • 8
  • 17
  • This solved my problem after I changed the comparison string to capitals. – sanepete Jul 10 '17 at 16:51
  • This is very interesting. I had tried this with '%[a-z]%', which does not work. I have confirmed that spelling out every letter does work. – RPh_Coder Sep 15 '20 at 17:41
9

IN MS SQL server use the COLLATE clause.

SELECT Column1
FROM Table1
WHERE Column1 COLLATE Latin1_General_CS_AS = 'casesearch'

Adding COLLATE Latin1_General_CS_AS makes the search case sensitive.

Default Collation of the SQL Server installation SQL_Latin1_General_CP1_CI_AS is not case sensitive.

To change the collation of the any column for any table permanently run following query.

ALTER TABLE Table1
ALTER COLUMN Column1 VARCHAR(20)
COLLATE Latin1_General_CS_AS

To know the collation of the column for any table run following Stored Procedure.

EXEC sp_help DatabaseName

Source : SQL SERVER – Collate – Case Sensitive SQL Query Search

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
subhash
  • 276
  • 1
  • 1
7

I've done something like this to find out the lower cases.

SELECT *
FROM YourTable
  where BINARY_CHECKSUM(lower(ColumnName)) = BINARY_CHECKSUM(ColumnName)
Mahib
  • 3,977
  • 5
  • 53
  • 62
3
mysql> SELECT '1234aaaa578' REGEXP '^[a-z]';
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
  • Will not work with any other characters than a-z, like åäöøüæï, etc. – geon Nov 08 '10 at 11:48
  • 1
    FYI. REGEXP and RLIKE use the current character set when deciding the type of a character. The default is latin1 (cp1252 West European). Warning The REGEXP and RLIKE operators work in byte-wise fashion, so they are not multi-byte safe and may produce unexpected results with multi-byte character sets. In addition, these operators compare characters by their byte values and accented characters may not compare as equal even if a given collation treats them as equal. – Elzo Valugi Nov 16 '10 at 15:24
3

I have to add BINARY to the ColumnX, to get result as case sensitive

SELECT * FROM MyTable WHERE BINARY(ColumnX) REGEXP '^[a-z]';
1

I'm not an expert on MySQL I would suggest you look at REGEXP.

SELECT * FROM MyTable WHERE ColumnX REGEXP '^[a-z]';
CJM
  • 11,908
  • 20
  • 77
  • 115
1

In Posgresql you could use ~

For example you could search for all rows that have col_a with any letter in lowercase

select * from your_table where col_a '[a-z]';

You could modify the Regex expression according your needs.

Regards,

Nicolas Finelli
  • 2,170
  • 1
  • 14
  • 9
1

--For Sql

SELECT *
FROM tablename
WHERE tablecolumnname LIKE '%[a-z]%';
toonice
  • 2,211
  • 1
  • 13
  • 20
1

Logically speaking Rohit's solution should have worked, but it didn't. I think SQL Management Studio messed up when trying to optimize this.

But by modifying the string before comparing them I was able to get the right results. This worked for me:

SELECT [ExternalId]
FROM [EquipmentSerialsMaster] where LOWER('0'+[ExternalId]) COLLATE Latin1_General_CS_AS != '0'+[ExternalId]
Robert Patterson
  • 509
  • 7
  • 12
1

This works in Firebird SQL, it should work in any SQL queries I believe, unless the underlying connection is not case sensitive.

To find records with any lower case letters:

select * from tablename where upper(fieldname) <> fieldname

To find records with any upper case letters:

select * from tablename where lower(fieldname) <> fieldname
D M
  • 5,769
  • 4
  • 12
  • 27
0

This worked for me

SELECT * FROM programs WHERE LOWER(CODE) <> CAST(CODE AS BINARY)
user1917451
  • 169
  • 2
  • 3
  • 11