String formatting in Oracle (PL /) SQL

Modern programming languages ​​allow the developer to create strings with placeholders and replace the correct values ​​with a function / method, commonly called format. Sometimes it looks like this:

"Hi {0}! How are you?".format('John');

Is there any function in Oracle SQL or PL / SQL with the same behavior? Or what is best here?

+4
source share
1 answer

utl_lms , and especially the format_message()procedure of this package can be used to format the string.

begin
  dbms_output.put_line(utl_lms.format_message('Hi %s! How are you %s?.'
                                             , 'John'
                                             , 'John'
                                             )
                       );
end;

Result:

Hi John! How are you John?.

It should be noted that:

  • It only works in the PLS / SQL block, not in SQL.
  • (%s , %d ), .
+10

All Articles