Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test for Upper Case - T-Sql

All,

How can I check if a specified varchar character or entire string is upper case in T-Sql? Ideally I'd like to write a function to test if a character is upper case, then I can later apply that to a generic varchar. It should return false for non alphabetic characters. I am only interested in english language characters.

I am working with T-sql in SQL Management Studio, and I have tried pulling records beginning with a lower case letter from a table in this fashion:

select * from TABLE where SUBSTRING(author,1,1) != LOWER(SUBSTRING(author,1,1)) 

Which returns 0 records, but I know there are records beginning with upper and lower case letters.

Thanks


EDIT: Since both podiluska and joachim-isaksoon have successfully answered my question (Both methods work for my purposes), would someone mind explaining which would be the most efficient method to use to query a table with a large number of records to filter out records with authors beginning with or without a capital letter?

like image 788
Danzomida Avatar asked Aug 08 '13 10:08

Danzomida


People also ask

How do I select case sensitive in SQL?

SQL Server is, by default, case insensitive; however, it is possible to create a case-sensitive SQL Server database and even to make specific table columns case sensitive. The way to determine if a database or database object is to check its "COLLATION" property and look for "CI" or "CS" in the result.

How do you find upper and lower case in SQL?

Case insensitive SQL SELECT: Use upper or lower functionsselect * from users where lower(first_name) = 'fred'; As you can see, the pattern is to make the field you're searching into uppercase or lowercase, and then make your search string also be uppercase or lowercase to match the SQL function you've used.

How do you capitalize in SQL?

If you want to display a string in uppercase, use the SQL UPPER() function. This function takes only one argument: the string column that you want to convert to uppercase.


1 Answers

Using collations

eg:

if ('a'='A' Collate Latin1_General_CI_AI)      print'same 1' else     print 'different 1'  if ('a'='A' Collate Latin1_General_CS_AI)      print'same 2' else     print 'different 2'  

The CS in the collation name indicates Case Sensitive (and CI, Case Insensitive). The AI/AS relates to accent sensitivity.

or in your example

SUBSTRING(author,1,1) <> LOWER(SUBSTRING(author,1,1)) COLLATE Latin1_General_CS_AI 
like image 53
podiluska Avatar answered Oct 05 '22 20:10

podiluska