Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL find string with lowercase and uppercase

Tags:

tsql

I have a database with several tables and I need to search every varchar column across the database, for columns that simultaneously contain lower and upper case characters.

To clarify:

If one column contains helLo the name of the column should be returned by the query, but if the column values only contain either hello or HELLO then the name of the column is not returned.

like image 318
user2332607 Avatar asked Aug 25 '16 15:08

user2332607


People also ask

How can we check both lowercase and uppercase in SQL?

Case sensitive search in SQL Server can be achieved either by using COLLATE or by using BINARY_CHECKSUM(). COLLATE is the T-SQL clause used to define collation. BINARY_CHECKSUM() is a built-in system function used to compare the binary check-sum value.

How do you find lowercase in SQL?

Use the SQL LOWER() function if you want to convert a string column to lowercase. This function takes only one argument: the column whose values you want to lowercase. This function is a good choice if your database is case sensitive and you want to select only records matching a particular string.

How do I select case sensitive in SQL?

The SQL case sensitivity can be set for non-keyword objects such as tables and columns by specifying the collation against the database, tables, and column level by mentioning CI or CS in it that stands for case insensitive and case sensitive, respectively.

How do I find the capital letters in SQL Server?

SQL Server UPPER() Function The UPPER() function converts a string to upper-case. Note: Also look at the LOWER() function.


2 Answers

Let's exclude all UPPER and all LOWER, the rest will be MIXED.

SELECT someColumn
FROM someTable
WHERE someColumn <> UPPER(someColumn) AND someColumn <> LOWER(someColumn) 

EDIT:

As suggested in comments and described in detail here I need to specify a case-sensitive collation.

SELECT someColumn
FROM someTable
WHERE someColumn <> UPPER(someColumn) AND 
      someColumn <> LOWER(someColumn) 
      Collate SQL_Latin1_General_CP1_CS_AS
like image 53
PM 77-1 Avatar answered Nov 10 '22 10:11

PM 77-1


It sounds like you are after a case sensitive search, so you'd need to use a case sensitive collation for there WHERE clause.

e.g. if your collation is currently SQL_Latin1_General_CP1_CI_AS which is case insensitive, you can write a case sensitive query using:

SELECT SomeColumn
FROM dbo.SomeTable
WHERE SomeField LIKE '%helLo%' COLLATE SQL_Latin1_General_CP1_CS_AS

Here, COLLATE SQL_Latin1_General_CP1_CS_AS tells it to use a case sensitive collation to perform the filtering.

like image 24
AdaTheDev Avatar answered Nov 10 '22 10:11

AdaTheDev