Skip to Main Content
Shuttle Sorting String Split - Arrays

Split email into First Last or Last, First

How to split email into First Last or Last, First

Example 1

DECLARE
    l_name VARCHAR2(255);
BEGIN
    SELECT
        initcap(replace(
            regexp_substr(:PN_EMAIL, '[^@]*'),
            '.',
            ' '
        )) AS name
    INTO l_name
    FROM
        dual;

    apex_util.set_session_state(
        p_name  => 'PN_EMAIL',
        p_value => l_name
    );
END;

 

Example 2

DECLARE
    l_name VARCHAR2(255);
BEGIN
    SELECT
            CASE
                WHEN regexp_substr(
                    initcap(replace(
                        regexp_substr(:PN_EMAIL, '[^@]*'),
                        '.',
                        ' '
                    )),
                    '([^ ]+)',
                    1,
                    3,
                    NULL
                ) IS NULL THEN
                    regexp_substr(
                        initcap(replace(
                            regexp_substr(:PN_EMAIL, '[^@]*'),
                            '.',
                            ' '
                        )),
                        '([^ ]+)',
                        1,
                        2,
                        NULL
                    )
                ELSE
                    regexp_substr(
                        initcap(replace(
                            regexp_substr(:PN_EMAIL, '[^@]*'),
                            '.',
                            ' '
                        )),
                        '([^ ]+)',
                        1,
                        3,
                        NULL
                    )
            END
            || ', '
            || regexp_substr(
            initcap(replace(
                regexp_substr(:PN_EMAIL, '[^@]*'),
                '.',
                ' '
            )),
            '([^ ]+)',
            1,
            1,
            NULL
        ) AS name
    INTO l_name
    FROM
        dual;

    apex_util.set_session_state(
        p_name  => 'PN_EMAIL',
        p_value => l_name
    );
END;