SQL SERVER – Puzzle – Incorrect Results with Decimal

It has been a while we have seen a puzzle on this blog and I keep on receiving emails and comments that you all would love to see one more puzzle. Let us see a new puzzle “Incorrect Results with Decimal”.

So the question is very simple and I expect everyone of you to get the correct answer without running the code actually in SSMS. However, due to any reason, you can’t figure out the answer, please do not hesitate to see the hints and run the entire query in SSMS.

First thing first lets us set up a table. The table contains three columns – ID, Price and Discount. We need to find out the total discount per item and final price of the product after discount.

-- Create table
CREATE TABLE #TestTable (ID INT, Price INT, Discount INT)
-- Populate table
INSERT INTO #TestTable (ID, Price, Discount)
SELECT 1, 1, 5
UNION ALL
SELECT 2, 2, 5
UNION ALL
SELECT 3, 3, 5
UNION ALL
SELECT 4, 4, 5
GO
-- Data in table
SELECT *
FROM #TestTable
GO

When we run above script we will get following answer.

SQL SERVER - Puzzle - Incorrect Results with Decimal puzzledecimal1

Now our puzzle is to find out discount value and total final price of the product.

Here is the script which is written to find TotalDiscount and FinalPrice. However, it is giving us all the answers as 0 (zero). The script is INCORRECT.

-- SELECT statement (Incorrect result)
SELECT ID, CAST((Price*Discount)/100 AS DECIMAL(10,2)) TotalDiscount,
		Price*CAST((Price*Discount)/100 AS DECIMAL(10,2)) FinalPrice
FROM #TestTable
GO

Here is the incorrect result.

SQL SERVER - Puzzle - Incorrect Results with Decimal puzzledecimal2

Puzzle Statement

Please fix the above script in such a way that it gives correct TotalDiscount and Final Price Value.

We are expecting the answer as in the following image.

SQL SERVER - Puzzle - Incorrect Results with Decimal puzzledecimal3

Puzzle Hint

If you can’t figure out the correct answer, here is another image which can potentially guide you with the correct answer.

SQL SERVER - Puzzle - Incorrect Results with Decimal puzzledecimal4

Let us see if you can give correct answer to this puzzle or not. Please share with your friends and test their knowledge about SQL as well.

NOTE: Please leave a correct answer in the comment section. I will publish all the comments together on coming Monday, till then all the answers will be hidden.

Reference: Pinal Dave (https://blog.sqlauthority.com)

SQL Datatype, SQL Function, SQL Scripts, SQL Server
Previous Post
SQL SERVER – Why We have Audit Trace in DATA Folder? What are These Files?
Next Post
SQL SERVER – Puzzle – Write a Shortest Code to Produce Zero

Related Posts

245 Comments. Leave new

  • Mangesh Bedarkar
    June 28, 2017 9:40 am

    Following query will given the output as expected..

    SELECT ID, CAST((Price*Discount)/100.00 AS DECIMAL(10,2)) TotalDiscount,
    Price-CAST((Price*Discount)/100.00 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable

    Reply
  • Aswini Kumar Tummala
    June 28, 2017 10:18 am

    Multiplication with 1.0.
    — SELECT statement (Incorrect result)
    SELECT ID, CAST((Price*Discount)*1.0/100 AS DECIMAL(10,2)) TotalDiscount,
    Price*CAST((Price*Discount)*1.0/100 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable
    GO

    Reply
  • SELECT ID, CAST((Price*Discount)/100.00 AS DECIMAL(10,2)) TotalDiscount,
    Price-CAST((Price*Discount)/100.00 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable
    GO

    Reply
  • We need to do implicit convertion by multiplying with 0.1

    SELECT ID, CAST((Price*Discount)*0.1/100 AS DECIMAL(10,2)) TotalDiscount,
            Price*CAST((Price*Discount)*0.1/100 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable
    GO

    Reply
  • Here is script with missing element

    select CAST(((Price*Discount)*1.00)/(100) AS DECIMAL(10,2)) TotalDiscount , Price-CAST(((Price*Discount)*1.00)/100 AS DECIMAL(10,2)) FinalPrice FROM #TestTable

    Reply
  • hello sir
    i have tried to find right answer as bellow

    SELECT ID, CAST(CAST((Price*Discount)AS DECIMAL(10,2))/100 AS DECIMAL(10,2)) TotalDiscount,
    CAST(Price – CAST(CAST((Price*Discount)AS DECIMAL(10,2))/100 AS DECIMAL(10,2))AS DECIMAL(10,2))FinalPrice
    FROM #TestTable
    GO

    from
    Asmita Patel
    Gujarat,India

    Reply
  • SELECT ID,convert(decimal(10,2),price*(convert(decimal(10,2),Discount)/100))TotalDiscount,Price-convert(decimal(10,2),Price*(convert(decimal(10,2),Discount)/100)) Finalprice FROM #TestTable

    Reply
  • Hello sir
    I have tried to solve it as below

    SELECT ID, CAST(CAST((Price*Discount)AS DECIMAL(10,2))/100 AS DECIMAL(10,2)) TotalDiscount,
    CAST(Price – CAST(CAST((Price*Discount)AS DECIMAL(10,2))/100 AS DECIMAL(10,2))AS DECIMAL(10,2))FinalPrice
    FROM #TestTable
    GO

    Thanking you

    Asmita Patel
    Gujarat,India

    Reply
  • SELECT ID, Cast (CAST((Price*Discount) AS DECIMAL(10,2))/Cast(100 AS DECIMAL(10,2)) AS DECIMAL(10,2)) TotalDiscount
    ,Cast (CAST(Price*(Price*Discount) AS DECIMAL(10,2))/Cast(100 AS DECIMAL(10,2)) As DECIMAL(10,2)) As FinalPrice
    FROM #TestTable
    GO

    For Decimal Result divided And divisor both Convert/Cast as Decimal

    Reply
  • SELECT ID, CAST((Price*Discount)*1.0/100 AS DECIMAL(10,2)) TotalDiscount,
    Price-CAST((Price*Discount)*1.0/100 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable;
    GO

    Reply
  • SELECT ID, CAST((Price*Discount)*1.00/100 AS DECIMAL(10,2)) TotalDiscount,
    Price*CAST((Price*Discount)*1.00/100 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable
    GO

    Reply
  • SELECT ID, CAST((Price*Discount)*1.00/100 AS DECIMAL(10,2)) TotalDiscount,
    Price – CAST((Price*Discount)*1.00/100 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable
    GO

    Reply
  • SELECT ID, CAST((Price*Discount)/100.0 AS DECIMAL(10,2)) TotalDiscount,
    Price*CAST((Price*Discount)/100.0 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable
    GO

    Reply
  • Manish Arvind
    June 28, 2017 2:00 pm

    Missin Element is ” * 1.0 ”
    [means we have to multiply with decimal number 1.0, so that we can get exact decimal result]

    Reply
  • Neil Macehiter
    June 28, 2017 2:16 pm

    — SELECT statement (Incorrect result)
    SELECT ID, CAST((Price*Discount)*1.00/100 AS DECIMAL(10,2)) TotalDiscount,
    Price-CAST((Price*Discount)*1.00/100 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable
    GO

    Reply
  • Michał Łabuda
    June 28, 2017 2:58 pm

    The shortest way i could come up with asap is to change type of divider so division result is not converted into integer before being casted to decimal.

    SELECT ID, CAST((Price*Discount)/100.0 AS DECIMAL(10,2)) TotalDiscount,
    Price*CAST((Price*Discount)/100.0 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable
    GO

    Reply
  • The cast is too late, the expression has already been evaluated as int. Replace 100 with 100.0 .

    Reply
  • Reply
  • SELECT ID, CAST((Price*Discount)/100.0 AS DECIMAL(10,2)) TotalDiscount,
    Price*CAST((Price*Discount)/100.0 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable

    Reply
  • SELECT ID, CAST((Price*Discount)/100.00 AS DECIMAL(10,2)) TotalDiscount,
    Price-CAST((Price*Discount)/100.00 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable
    GO

    Reply

Leave a Reply