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

  • Shubham Rajput
    June 28, 2017 6:28 pm

    Dividend and divisor both are int type . And output will come as int. So we have to cast on of them in decimal explicitly.

    Reply
  • Nice quiz !

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

    I use *.01 instead of /100 since it avoids divide/0 errors.

    Reply
  • Gajendra Singh
    June 28, 2017 6:33 pm

    Same result can be achieved by changing the query like
    SELECT ID, CAST(convert(decimal,Price*Discount)/100 AS DECIMAL(10,2)) TotalDiscount,
    Price-CAST(convert(decimal,Price*Discount)/100 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable

    Reply
  • Ganesh Mohite
    June 28, 2017 6:34 pm

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

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

    Reply
  • Antonio Bravo
    June 28, 2017 6:37 pm

    The problem is that you are operating with internet and none of the digits is a decimal that’s why you get 0, to get the correct answer you should divide by 100.0

    I love the puzzles that you ask Pinal.

    Thanks
    Antonio

    Reply
  • (price*discount) *1.0/100
    Or
    Cast((price*discount) as decimal)

    Reply
  • Manish Kumar
    June 28, 2017 6:45 pm

    Hi,

    I am not sure whether it correct or not, but it may be like:

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

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

    Reply
  • One way to do it is like so…

    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

    But I’m sure this isn’t the best solution

    Reply
  • Ranga Maddileti
    June 28, 2017 6:47 pm

    SELECT Id
    ,Price
    ,Discount
    ,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,
    CAST(((Price-(Price*Discount)*1.00/100)) AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable
    GO

    Reply
  • 0.98.
    SELECT ID, CAST((Price*Discount)*0.98/100 as decimal(10,2)) TotalDiscount,
    Price-CAST((Price*Discount)*0.98/100 as decimal(10,2)) FinalPrice
    FROM #TestTable

    Reply
  • David Wallace
    June 28, 2017 6:49 pm

    — 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)*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 statement (Incorrect result)
    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
  • It’s the classic integer division issue, that is a common question on programming interviews.

    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

    Reply
  • Shashank Kumar Patel
    June 28, 2017 6:54 pm

    We need to cast each value while calculating ..

    Below is the answer.

    CREATE TABLE #TESTABLE (ID INT, PRICE INT, DISCOUNT INT)

    INSERT INTO #TESTABLE (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

    SELECT
    ID,
    CAST(CAST(PRICE*DISCOUNT AS DECIMAL(10,2))/100 AS DECIMAL(10,2)) TOTALDISCOUNT,
    PRICE – CAST(CAST(PRICE*DISCOUNT AS DECIMAL(10,2))/100 AS DECIMAL(10,2)) FINALPRICE
    FROM #TESTABLE

    DROP TABLE #TESTABLE

    Reply
  • subhojeetsql
    June 28, 2017 6:55 pm

    INT divided by INT is always an INT.. So TotalDiscount is calculated as 0. Hence all result is 0. we can multiply (Price*Discount) by 1.0 and we will get the correct results.

    — 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
  • Alfaiz Ahmed
    June 28, 2017 6:56 pm

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

    Reply

Leave a Reply