OraQA

Oracle Question and Answer


Latest Comments

  • Laurent Schneider:
    if you like a Base64 format, maybe this… select utl_raw.cast_to_varchar 2(...

  • hsafra:
    You need to give more specs for the question: What letter are acceptable? What letters aren’t? Do you...

  • ragunathansd:
    I am not inserting sequence numbers from database. I need to populate the data in a grid. If a user...

  • gamyers:
    “redo log file gets full” That is the nature of a redo log file. It gets full, switched and...

Comments RSS feed


  • Do you have a solution to a problem? Do you have an unanswered question? Login and share it with the Oracle community. More...

Oracle News


Entries RSS feed

Comments RSS feed

How to determine if the formula string contains balanced pairs of parentheses in SQL

June 8th, 2008 By Frank Zhou

The following SQL pattern can be used to determine whether the formula/expression string contains balanced pairs of parentheses.

create table test as
select '( (1+2)*8 + ( (3+4)+(5+6) ) /9 ) * 9- (7+8)' str_num from dual
union all
select '( (1+2)+ (3+4) * 5 ) +6 ) ' str_num from dual ;

——————-SQL Solution —————-

SELECT old_str_num, flag as balanced_parentheses
FROM
(
 SELECT str_num as old_str_num, rownum rn,
        regexp_replace(str_num,'[^()]') str_num
 FROM test
)
MODEL
PARTITION BY (rn)
DIMENSION BY (0 dim)
MEASURES(old_str_num, str_num, CAST(NULL AS VARCHAR2(1)) flag )
RULES ITERATE (10000) UNTIL (str_num[0] IS NULL OR flag[0] = 'F')
(
 flag[0] = CASE WHEN regexp_like(str_num[cv()],'^\(.*\)$')AND
                     (length(regexp_replace(str_num[cv()], '\(')) =
                      length(regexp_replace(str_num[cv()], '\)'))
                     )
                THEN 'T'
                ELSE 'F' END,
 str_num[0] = CASE WHEN flag[0] = 'T'
                   THEN regexp_replace(str_num[cv()], '\(\)')
               END
);

OLD_STR_NUM                                   BALANCED_PARENTHESES
-------------------------------------------- -------------------------
( (1+2)*8 + ( (3+4)+(5+6) ) /9 ) * 9- (7+8)       T
( (1+2)+ (3+4) * 5 ) +6 )                         F

2 Responses to “How to determine if the formula string contains balanced pairs of parentheses in SQL”

  1. gmyers Says:

    Or a simple
    select str_num,
    case when length(replace(str_num,’)')) = length(replace(str_num,’('))
    then ‘T’ else ‘F’ end bal
    from test

  2. Frank Zhou Says:

    Gmyers,

    Your simpler method doesn’t work,
    it didn’t put all cases into consideration.

    For example when str_num = ‘1+)2+(3+4′

    SQL> select str_num,
    2 case when length(replace(str_num,’)')) = length(replace(str_num,’('))
    3 then ‘T’ else ‘F’ end bal
    4 from ( select ‘1+)2+(3+4′ str_num from dual);

    STR_NUM BAL
    ——— —–
    1+)2+(3+4 T

    The correct answer should be “F”.

    Frank

Leave a Reply

You must be logged in to post a comment.

RSS feed for comments on this question