DBAsupport.com Forums - Powered by vBulletin
Results 1 to 6 of 6

Thread: How do I count number of characters?

  1. #1
    Join Date
    Jan 2001
    Posts
    515

    How do I count number of characters?

    If I have a string "server1/server2/server3". Is there a function that can count the number of forward slashes "/" ?

  2. #2
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    Probably not the most elegant but, it's functional...

    Code:
    SQL> 
    SQL> select  CHR, CNT
      2  from (
      3  select  SUBSTR(mystr , level , 1) "CHR", 
      4          count(*)                  "CNT"
      5  from   (select 'server1/server2/server3' as mystr from dual) 
      6  connect by prior mystr = mystr 
      7  and level <= LENGTH(mystr) 
      8  and prior dbms_random.string ('p', 10) is not null 
      9  group by SUBSTR(mystr , level , 1) 
     10  order by SUBSTR(mystr , level , 1)
     11  )
     12  where CHR = '/'
     13  ; 
    
    C        CNT
    - ----------
    /          2
    
    SQL>
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  3. #3
    Join Date
    Jan 2001
    Posts
    2,828
    Hi

    if you are lucky and are on 11g then regexp_count also might be worth a try.

    for a 10g solution try the one below
    http://www.sqlsnippets.com/en/topic-12818.html

    in 9i and below you should be still be able to use instr perhaphs

    regards
    Last edited by hrishy; 02-28-2010 at 10:34 AM.

  4. #4
    Join Date
    Jul 2002
    Location
    Lake Worth, FL
    Posts
    1,492

    Cool Test of time

    How about the time tested alternative:
    Code:
    SQL> SELECT Length('server1/server2/server3')
      2>      - Length(Replace('server1/server2/server3','/',''))
      3>   FROM dual;
    
    LENGTH(:X)-LENGTH(REPLACE(:X,'/',''))
    -------------------------------------
                                        2
    
    SQL>
    "The person who says it cannot be done should not interrupt the person doing it." --Chinese Proverb

  5. #5
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    Quote Originally Posted by LKBrwn_DBA View Post
    How about the time tested alternative
    clever!
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  6. #6
    Join Date
    Nov 2000
    Location
    Pittsburgh, PA
    Posts
    4,166
    That really is clever!!!

    Code:
    SYS AS SYSDBA> SELECT         Length('server1/server2/server3/server4/server1/server2/server3/server4/')
         - Length(Replace('server1/server2/server3/server4/server1/server2/server3/server4/','/','')) counter
      FROM dual;
      2    3  
       COUNTER
    ----------
             8
    
    Elapsed: 00:00:00.02

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width