Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need help in using REGEXP function

SELECT ID,
     CASE WHEN listagg(
            DISTINCT col_1,',') WITHIN GROUP(ORDER BY col_1)= '' THEN 'null' 
            ELSE (lower(LISTAGG(distinct col_1,',') WITHIN GROUP ( ORDER BY col_1))) END AS Col_001
        FROM 
            (SELECT distinct B.ID, date, timestamp, 
            TRY_CAST(pno as INTEGER) as pno,
            REGEXP_REPLACE(col_1,'\http.*$|null', '') as col_1
            FROM 
            table1 B LEFT JOIN table2 D ON D.ID=B.ID
            WHERE B.ID IN('5871162','35915895')
            and date='2021-11-02'
            ORDER BY pno) 
            GROUP BY ID;

When I run the above query, I’m getting results like

ID                 COL_001

5871162            ,monthend_offer
35915895           dec_cashback,dec_offer

If I replace comma with empty string, the result will be like mentioned below and that is not the excepted result

    5871162            monthend_offer
    35915895           dec_cashbackdec_offer

I want to replace only the comma ‘,’ from the first record. The record should display like below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

5871162            monthend_offer
35915895           dec_cashback,dec_offer

I want to replace only the LEADING comma

Any guidance on how to implement this?

>Solution :

SELECT 
    column1, 
    ltrim(column1, ','),
    regexp_replace(column1, '^,')
FROM VALUES
    (',monthend_offer'),
    ('dec_cashback,dec_offer');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading