Skip to main content

More readable code via APEX_STRING

W

We all have to concatenate strings and varibles and you have to admit that it will get ugly very quickly. Luckily someone created a sprint function we know from other programming languages.

It is called APEX_STRING.FORMAT.


Lets jump to the example first:

What would you rather to read and maintain, before or after version?


If you like the before version, you can stop reading. And if you are still here then we can be friends :-)

You can pass up to 20 arguments and you have two options how to reference them. You either use "%s" and then the values are replaced in the same order as you passed them. Or you can use numbers like "%0..19" and reuse them however you need. I usually stick with "%s" unless I need to replace same value multiple times.

There is a nice side effect of this type of concatenation. It is easy to run. Just copy paste the code, replace "!" with nothing, replace "%" with "&" and you can actually execute the query. You can't do it this simple with normal/common method.


Update, something you can copy paste:

    v_query := TRIM(APEX_STRING.FORMAT(q'!
        !%0
        !%1
        !%2
        !',
        --
        p0 => '',
        p1 => '',
        p2 => '',
        --
        p_max_length    => 32767,  -- be aware of weird 1000 as default
        p_prefix        => '!'
    ));


Comments