Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2016
    Posts
    5

    Answered: How to overloading function with different precision and scale of a DECIMAL data type

    Hi export
    I want to implement a user-defined function accept a DECIMAL as input parameter, and the parameter may have different precision and scale value.
    I looked into DB2 UDF strategy, it does support function overloading, but the parameter length and scale are ignored when verifying the function signature.
    For example: create function foo(decimal(6, 2)) and create function foo(decimal) has same signature, DB2 will report function redefined.

    Any body can help on this; any comments/suggestions are welcome.
    Basically, i want to implement a function like DB2 built-in function char(...), it could accept any kind of parameter.
    Thanks.

  2. Best Answer
    Posted by mark.bb

    "Hi,

    Try this:
    create function myfunc (a decfloat) ...
    You can call it like this afterwards:
    values (myfunc(dec(1.2345, 5, 4)), myfunc(dec(1.2345, 10, 5))"


  3. #2
    Join Date
    Jul 2016
    Location
    Moscow
    Posts
    110
    Provided Answers: 13
    Hi,

    Try this:
    create function myfunc (a decfloat) ...
    You can call it like this afterwards:
    values (myfunc(dec(1.2345, 5, 4)), myfunc(dec(1.2345, 10, 5))
    Regards,
    Mark.

  4. #3
    Join Date
    Nov 2016
    Posts
    5

    Thumbs up

    Quote Originally Posted by mark.bb View Post
    Hi,

    Try this:
    create function myfunc (a decfloat) ...
    You can call it like this afterwards:
    values (myfunc(dec(1.2345, 5, 4)), myfunc(dec(1.2345, 10, 5))
    Thanks, Mark, it works.

  5. #4
    Join Date
    Nov 2016
    Posts
    5
    Quote Originally Posted by mark.bb View Post
    Hi,

    Try this:
    create function myfunc (a decfloat) ...
    You can call it like this afterwards:
    values (myfunc(dec(1.2345, 5, 4)), myfunc(dec(1.2345, 10, 5))
    Mark, thanks. following your guide, i could move forward, but now another issue is met

    The DB2 function is defined as
    CREATE FUNCTION MY_TOCHAR(P DECFLOAT)
    RETURNS VARCHAR(50)
    LANGUAGE SQL
    RETURN VARCHAR(P);

    And i using this command to query a value:
    db2 => select MY_TOCHAR(dec(12.34, 4, 2)) from sysibm.sysdummy1

    --------------------------------------------------
    1.234E1

    1 record(s) selected.

    Unfortunately, it return me with "1.234E1", i wish it could return me the original format, i.e,. "12.34".

  6. #5
    Join Date
    Nov 2016
    Posts
    5
    I make it works.

    1. select MY_TOCHAR(DEC(12.34, 4, 2)) from sysibm.sysdummy1
    return "1.234E1"
    2. select MY_TOCHAR(DECFLOAT(DEC(12.34, 4, 2))) from sysibm.sysdummy1
    return "12.34"

    i don't know why, but anyway, the second one can work.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •