Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a character is uppercase or lowercase in postgresql?

I have failed to find any function like isupper or islower in postgresql. What I actually need is to select all the records from a table, where one of the columns contains capitized (but not uppercase) words. That is, the first symbol of each word is uppercase, and the second is lowercase. Words can be written in any language.

like image 517
Pupkov-Zadnij Avatar asked Dec 04 '12 16:12

Pupkov-Zadnij


People also ask

How do you know if a character is uppercase or lowercase?

Check the ASCII value of each character for the following conditions: If the ASCII value lies in the range of [65, 90], then it is an uppercase letter. If the ASCII value lies in the range of [97, 122], then it is a lowercase letter. If the ASCII value lies in the range of [48, 57], then it is a number.

How do I get uppercase in PostgreSQL?

Use the INITCAP() function to convert a string to a new string that capitalizes the first letter of every word. All other letters will be lowercase.

How do you check if a character in a string is uppercase?

To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase.

Is Postgres case sensitive?

PostgreSQL names are case sensitive. By default, AWS Schema Conversion Tool (AWS SCT) uses object name in lowercase for PostgreSQL. In most cases, you'll want to use AWS Database Migration Service transformations to change schema, table, and column names to lower case.


2 Answers

You can use Postgres regexp to test for your specific condition:

select * from sample 
where col ~ E'^[[:upper:]][^[:upper:]]'

You could use E'^[[:upper:]][[:lower:]]' if the second character must be lowercase alpha instead of any non-uppercase.

like image 169
dbenhur Avatar answered Sep 30 '22 04:09

dbenhur


If you want to know whether a string contains at least one lower case character then you can use the upper function [upper(mystr)=mystr]:

dbname=> select upper('AAbbCC')='AAbbCC';
 ?column? 
----------
 f
(1 row)

dbname=> select upper('AABBCC')='AABBCC';
 ?column? 
----------
 t
(1 row)

You can use the same logic for checking that a string contains at least one upper case character with the lower() sql function.

For more complicated pattern, you will need to use regular expression or substring as proposed by earlier answers.

like image 30
Kemin Zhou Avatar answered Sep 30 '22 02:09

Kemin Zhou