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

  • —- SELECT statement (correct result)
    SELECT ID, CAST((CAST(Price AS DECIMAL(4,2)) * Discount)/100 AS DECIMAL(4 ,2)) AS TotalDiscount,
    price-CAST((CAST(Price AS DECIMAL(4,2)) * Discount)/100 AS DECIMAL(4 ,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

    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
  • 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
  • adaikalapraveen
    June 28, 2017 7:58 pm

    SELECT CAST(CAST(Price * Discount As float) / 100 As decimal(8, 2)) from #TestTable

    Reply
  • Vijaykumar Ramteke
    June 28, 2017 7:58 pm

    Correct SQL is
    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
  • Converting price to decimal will give the expected output:

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

    Reply
  • Erich Göppel
    June 28, 2017 8:05 pm

    Hi Pinal
    one correct script would be :

    — SELECT statement (correct 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.0 AS DECIMAL(10,2)) TotalDiscount,
    Price*CAST((Price*Discount)/100.0 AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable
    GO

    Reply
  • SELECT z.p [ID], z.d [TotalDisco…], (CAST(z.p AS DECIMAL(10,2)) – CAST(z.d AS DECIMAL(10,2))) [FinalPrice]
    FROM (SELECT 1 as p, 0.05 as d
    UNION
    SELECT 2 as p, 0.10 as d
    UNION
    SELECT 3 as p, 0.15 as d
    UNION
    SELECT 4 as p, 0.20 as d) z

    Reply
  • Robert McCormick
    June 28, 2017 8:12 pm

    If you divide an integer by an integer, the result will be an integer. Add ‘.0’ to the end of each operation where you divide by 100 and you’ll get the answer you seek.
    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
  • MR O AIMIUWU
    June 28, 2017 8:32 pm

    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
  • Gabriele Massari
    June 28, 2017 8:32 pm

    Well… you could either multiply by 1.0 or divide by 100.0 (I prefer the latter, IDK if it has any downside compared to the first one). Also, as per your hint, the final price is given by a subtraction, not a multiplication. Here is the SELECT statement as I would write it:

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

    Reply
  • integer/integer return integer so multiple numerator with 1.0 then converted numerator value as decimal, decimal/integer return decimal value.

    Reply
  • Wow, what a cool puzzle.

    — 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
  • — SELECT statement (Incorrect result)
    SELECT ID, price, discount,
    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
  • I should have read the blog instead of just the email.

    — 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 ID, CAST(Price as DECIMAL(10,2)) Price,
    CAST((CAST(Price as DECIMAL(10,2))*(CAST(Discount AS DECIMAL(10,2))/100.00)) AS DECIMAL(10,2)) Discount,
    CAST((CAST(Price as DECIMAL(10,2))*(1.00 – (CAST(Discount AS DECIMAL(10,2))/100.00))) AS DECIMAL(10,2)) FinalPrice
    FROM #TestTable
    GO

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

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

    Reply
  • You could put in “*1.0” without quotes, of course and that would get you the correct answer, but why not just use 100.0 such that the result data type would be decimal?

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

    Reply

Leave a Reply