Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find values in all caps in SQL Server?

How can I find column values that are in all caps? Like LastName = 'SMITH' instead of 'Smith'

Here is what I was trying...

SELECT *   FROM MyTable  WHERE FirstName = UPPER(FirstName) 
like image 709
daveomcd Avatar asked Nov 17 '11 15:11

daveomcd


People also ask

How do I find the capital letters in SQL Server?

Example 2: Use UPPER function with all lower case characters in an expression. In this example, we use a string with Upper function, and it converts all letters in the string to uppercase. SELECT UPPER('learn sql server with sqlshack'); Output: It converts all characters for a string.

How do I select uppercase in SQL?

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

How do you find lowercase and uppercase in SQL?

Case insensitive SQL SELECT: Use upper or lower functions or this: select * 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 I find lowercase letters in SQL?

The SQL LOWER function converts all the characters in a string into lowercase. If you want to convert all characters in a string into uppercase, you should use the UPPER function. The following illustrates the syntax of the LOWER function. The LOWER function returns a string with all characters in the lowercase format.


2 Answers

You can force case sensitive collation;

select * from T   where fld = upper(fld) collate SQL_Latin1_General_CP1_CS_AS 
like image 145
Alex K. Avatar answered Sep 29 '22 00:09

Alex K.


Try

 SELECT *   FROM MyTable  WHERE FirstName = UPPER(FirstName) COLLATE SQL_Latin1_General_CP1_CS_AS 

This collation allows case sensitive comparisons.

If you want to change the collation of your database so you don't need to specifiy a case-sensitive collation in your queries you need to do the following (from MSDN):

1) Make sure you have all the information or scripts needed to re-create your user databases and all the objects in them.

2) Export all your data using a tool such as the bcp Utility.

3) Drop all the user databases.

4) Rebuild the master database specifying the new collation in the SQLCOLLATION property of the setup command. For example:

Setup /QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=InstanceName  /SQLSYSADMINACCOUNTS=accounts /[ SAPWD= StrongPassword ]  /SQLCOLLATION=CollationName 

5) Create all the databases and all the objects in them.

6) Import all your data.

like image 39
Joe Ratzer Avatar answered Sep 29 '22 01:09

Joe Ratzer