toDecimal function in dataflow of ADF/Azure Synapse Analytics not working correctly

Peruka, Sikander reddy 110 Reputation points
2025-02-20T15:15:57.9966667+00:00

I have input string field with name COST with below values.

COST


2753000

217400

500

252567552

I am using toDecimal function in derived Column transformation in dataflow as below.

z_COST= toDecimal(trim(COST))

Output is as below

z_COST


27530.00

2174.00

500.00

NULL

Why is the toDecimal('252567552') returned as NULL?

How can this be returned as 2525675.52 instead?

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,272 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Smaran Thoomu 20,305 Reputation points Microsoft Vendor
    2025-02-20T16:46:33.8233333+00:00

    Hi @Peruka, Sikander reddy
    Welcome to Microsoft Q&A platform.

    Thank you for reaching out and providing detailed information about the issue you're encountering with the toDecimal function in Azure Data Factory (ADF) or Azure Synapse Analytics.

    Understanding the Issue:

    When converting the string '252567552' using toDecimal(trim(COST)), the function returns NULL instead of the expected 2525675.52. This discrepancy arises because, by default, the toDecimal function in ADF assumes a precision of 10 and a scale of 2 (decimal(10,2)). In this context:

    • Precision (10): Total number of digits allowed, both before and after the decimal point.
    • Scale (2): Number of digits allowed after the decimal point.

    The number '252567552' exceeds the default precision, leading to a NULL result upon conversion.

    Proposed solution:

    To correctly format your numbers while ensuring that only necessary values are adjusted, you can use a conditional expression in your Derived Column transformation:

    User's image

    iif(length(COST) > 8, toDecimal(toInteger(COST) / 100, 12, 2), toDecimal(COST, 12, 2))
    
    • For numbers longer than 8 digits, it assumes the last two digits should be after the decimal and divides by 100.
    • For shorter numbers, it keeps them unchanged.

    Expected Output:

    User's image

    This should resolve the issue while keeping other numbers accurate. Let me know if you need further clarification!

    Hope this helps. Do let us know if you any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.