Oracle 'printf' equivalent

前端 未结 5 1546
时光说笑
时光说笑 2021-01-04 07:31

Is there an equivalent or alternative to the following?

SELECT mix_type || \' (\' || mix_num || \')\' as description
  FROM acid_batch
 WHERE mix_num < 10         


        
5条回答
  •  佛祖请我去吃肉
    2021-01-04 07:54

    Just another idea for you: I've found REPLACE to be useful for this kind of thing, especially when the template is complex:

    SELECT REPLACE(REPLACE(
            '%mix_type% (%mix_num%)' /*template*/
           ,'%mix_type%', mix_type)
           ,'%mix_num%' , mix_num ) as description,
    FROM   acid_batch
    WHERE  mix_num < 10
    

    The only downside is you need to add as many REPLACE('s as there are variables to replace - but at least you only need to have one per variable, regardless of how many times it appears in the template.

    (NOTE: There is no particular significance to using "%" as a delimiter, it's just a personal convention of mine - you might choose a different pattern, e.g. or [mix_type])

    For this particular instance it looks like overkill, but in some cases it can make things much easier, e.g.:

    template := 'bla bla %a% %b% %a%';
    output := REPLACE(REPLACE(template
        ,'%a%', some_complex_expression)
        ,'%b%', b);
    

    Compare the above with:

    output := 'bla bla ' || some_complex_expression || ' ' || b || ' ' || some_complex_expression;
    

提交回复
热议问题