简体   繁体   中英

Find column values where first letter is upper case

I have a table EMP_INFO with EID, ENAME, GENDER. My objective is to display only those ENAME values where the first letter is Capital or uppercase.

Table like:

EID     ENAME     GENDER
001     Samuel    M
002     john      M
003     susan     F
004     CALEB     M

Desired output like:

EID    ENAME
001    Samuel
004    CALEB

I have tried:

SELECT EID, ENAME
FROM EMP_INFO
WHERE ENAME like '[A-Z]%';

But this is just giving a blank output. No errors, no warnings but no output as well. Also I am using oracle sql developer.

Oracle does not support wildcards in the LIKE pattern. You can use regular expressions instead:

select EID , ENAME
from EMP_INFO
where regexp_like(ENAME, '^[A-Z]');

Alternatively, you could just compare the first character:

where substr(ENAME, 1, 1) BETWEEN 'A' AND 'Z'

Here is a working example of this version.

By default, Oracle is case-sensitive, so these should work on most Oracle systems.

We have a well-known function called initcap to be considerable :

SELECT EID, ENAME
  FROM EMP_INFO
 WHERE substr(ENAME,1,1) = substr(initcap(ENAME),1,1);

or alternatively use :

SELECT EID, ENAME
  FROM EMP_INFO
 WHERE ENAME between chr(65) and chr(92);  

SQL Fiddle Demo

You might try something like the following (assuming you've not done anything to make your SQL queries case-insensitive ):

SELECT eid, ename
  FROM emp_info
 WHERE ename >= 'A'
   AND ename < CHR(ASCII('Z')+1);

This will ensure that the first character of ename falls between A and Z inclusive. The value of CHR(ASCII('Z')+1) is [ but that's not terribly important - I think it's clearer to use the functions in this case than the "magic character".

EDIT: The reason this works is that lower-case characters, as a group, appear after upper-case characters in many character sets*, so as long as a value of ename is between A and Z inclusive, according to the typical string comparison, it will start with an upper-case character.

*For example, here is the Unicode character table and here is the ASCII character table . I suspect that this solution may not work with EBCDIC character sets but I don't have a server handy on which I can confirm that suspicion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM