Home » SQL & PL/SQL » SQL & PL/SQL » Find UPPER Chars in a name column (11g)
Find UPPER Chars in a name column [message #622464] Tue, 26 August 2014 13:33 Go to next message
Yuvraaj
Messages: 122
Registered: January 2011
Location: California, USA
Senior Member
Hello,

I'm trying to find the upper chars in a name field, but am getting whole word instead only UPPER case chars.

Please let me know what am missing.

sql>CREATE TABLE EMP(ENAME VARCHAR2(30));

Table created.

Elapsed: 00:00:00.15
sql>
sql>
sql>INSERT INTO EMP VALUES('JanuAry');

1 row created.

Elapsed: 00:00:00.09
sql>INSERT INTO EMP VALUES('mArcH');

1 row created.

Elapsed: 00:00:00.10
sql>
sql>INSERT INTO EMP VALUES('JulY');

1 row created.

Elapsed: 00:00:00.08


sql>INSERT INTO EMP VALUES('MAY'); 

1 row created.

Elapsed: 00:00:00.08
sql>INSERT INTO EMP VALUES('june');

1 row created.

Elapsed: 00:00:00.08
sql>select * from emp;

ENAME
------------------------------
JanuAry
mArcH
JulY
MAY
june

Elapsed: 00:00:00.17

sql>  select ename from  
    (select REGEXP_SUBSTR (ename, '[^ ]+', 1, level) ename
     from EMP
     connect by level <= length(regexp_replace(ename,'[^ ]*'))+1
    )
 where regexp_instr(ename,'[A-Z]') > 0;  2    3    4    5    6  

ENAME
------------------------------------------------------------------------------------------
JanuAry
mArcH
JulY
MAY

Elapsed: 00:00:00.17
sql>


Expected:
---
JA
AH
JY
MAY
---

Thanks.
Re: Find UPPER Chars in a name column [message #622465 is a reply to message #622464] Tue, 26 August 2014 13:38 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
your expectations are unwarranted & misguided when you "select ename" which returns the whole column ENAME.
You are doing NOTHING to exclude lower case letters from ENAME in SELECT clause
Re: Find UPPER Chars in a name column [message #622467 is a reply to message #622464] Tue, 26 August 2014 13:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Do NOT post the execution of the test case, just the statements so we can copy and paste to execute it.
Of course, test your test case before posting it.

You made no change on ename so why do you want Oracle displays it differently what you insert?
Your query displays the words that contains at least one upper character (and as all ename are single words then it displays these ones).

Re: Find UPPER Chars in a name column [message #622468 is a reply to message #622467] Tue, 26 August 2014 13:55 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Also do NOT use standard table names for your test case, like EMP, DEPT...

SQL> col upchar format a10
SQL> with
  2    data as (
  3      select ename, substr(ename,column_value,1) ch, column_value cn
  4      from emp,
  5           table(cast(multiset(select level from dual connect by level <= length(ename))
  6                      as sys.odciNumberList))
  7      where substr(ename,column_value,1)=upper(substr(ename,column_value,1))
  8    )
  9  select ename, listagg(ch) within group (order by cn) upchar
 10  from data
 11  group by ename
 12  order by ename
 13  /
ENAME                          UPCHAR
------------------------------ ----------
JanuAry                        JA
JulY                           JY
MAY                            MAY
mArcH                          AH

I bet Solomon will come with a nice query using XML features/functions avoiding the split+merge query. Smile

[Updated on: Tue, 26 August 2014 13:56]

Report message to a moderator

Re: Find UPPER Chars in a name column [message #622469 is a reply to message #622468] Tue, 26 August 2014 13:58 Go to previous messageGo to next message
Yuvraaj
Messages: 122
Registered: January 2011
Location: California, USA
Senior Member
Sure. Thanks.
Re: Find UPPER Chars in a name column [message #622470 is a reply to message #622469] Tue, 26 August 2014 14:10 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
SQL> select translate(ename,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ') from emp;

TRANSLATE(ENAME,'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ','ABCDEFGH
--------------------------------------------------------------------------------
JA
AH
JY
MAY
Re: Find UPPER Chars in a name column [message #622472 is a reply to message #622468] Tue, 26 August 2014 15:03 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Michel Cadot wrote on Tue, 26 August 2014 14:55

I bet Solomon will come with a nice query using XML features/functions avoiding the split+merge query. Smile



Well, there is no need to split string into characters, find upper-cased ones and concatenate them. OP was very close with regexp solution:

with t as (
           select 'JanuAry' month from dual union all
           select 'mArcH' from dual union all
           select 'JulY' from dual union all
           select 'MAY' from dual union all
           select 'june' from dual
          )
select  regexp_replace(month,'[^A-Z]') month
  from  t
  where regexp_replace(month,'[^A-Z]') is not null
/

MONTH
-------
JA
AH
JY
MAY

SQL> 


SY.
Re: Find UPPER Chars in a name column [message #622483 is a reply to message #622472] Wed, 27 August 2014 00:41 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
SQL> column u_str format A10;
SQL> with t as (
  2             select 'JanuAry' str from dual union all
  3             select 'mArcH' from dual union all
  4             select 'JulY' from dual union all
  5             SELECT 'MAY' FROM DUAL UNION ALL
  6             SELECT 'june' FROM DUAL UNION ALL
  7             select 'juneJUNE' from dual
  8            )
  9  SELECT Regexp_replace(str, '[a-z]', '') u_str
 10  FROM   t;

U_STR
----------
JA
AH
JY
MAY

JUNE

6 rows selected.


Regards,
Lalit
Re: Find UPPER Chars in a name column [message #622485 is a reply to message #622483] Wed, 27 August 2014 00:54 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Solomon's solution is better as it does not make any assumption on characters that are not upper case one.
Yours assumes that these other characters are only minuscules.
As OP's query (even wrong) checks white spaces, we can assume there can be at least some white spaces.

Previous Topic: Code Convertions to Oracle 7.3.4
Next Topic: Query to display string in vertical form
Goto Forum:
  


Current Time: Thu Apr 25 04:09:00 CDT 2024