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

  • Below are the two queries

    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

    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 #TestTable
    GO

    Reply
  • *1.0

    Reply
  • Sakaravarthi J
    June 28, 2017 5:04 pm

    The correct answer is:
    –================
    — 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

    /*
    The data type of the Price and Discount columns are created as INT so INT*INT will return INT. so we are casting the results to decimals ((Price*Discount)*1.00)<=casting INT value to Decimal (this is the actual casting unless the divide will not work correctly)
    */

    Reply
  • Hi Pinal, The missing element is *1.00

    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
  • — 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

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

    Reply
  • amol renkuntwar
    June 28, 2017 6:12 pm

    THE CORRECT ANSWER

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

    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

    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
  • haha, this is a mistake I sometimes meet or do when I am sleepy… The cast/convert is done too late and the result of the division is an integer and so the decimal are removed.
    Just add *1.00 to the numerator of the division and then the result will be decimal and will be casted afterwards

    Reply
  • Joe Gakenheimer
    June 28, 2017 6:15 pm

    All I did was cast the Discount to Decimal(10, 2) for the TotalDiscount and the FinalPrice and it worked as intended. You “Missing Element” is different than what I found as a solution; so I’ll be interested to see what it is.

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

    Reply
  • Vishal Gandhi
    June 28, 2017 6:19 pm

    SELECT *,CONVERT(DECIMAL(10,2),CONVERT(DECIMAL(10,2),Price * Discount) / 100),
    Price – CONVERT(DECIMAL(10,2),CONVERT(DECIMAL(10,2),Price * Discount) / 100)
    FROM #TestTable

    Reply
  • Bidyut Sarkar
    June 28, 2017 6:20 pm

    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

    Alternate Solution

    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
  • SELECT ID,
    cast(((CAST(Price AS DECIMAL(10,2))) * cast(Discount AS DECIMAL(10,2))/100) as decimal(10,2)) TotalDiscount,
    cast(CAST(Price AS DECIMAL(10,2)) – ((CAST(Price AS DECIMAL(10,2))) * cast(Discount AS DECIMAL(10,2))/100) as decimal(10,2)) as FinalPrice
    FROM #TestTable
    GO

    Reply
  • Milen Petrov
    June 28, 2017 6:21 pm

    Hi all,

    this is my quick answer:

    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
  • Parthasarathy J S
    June 28, 2017 6:22 pm

    Method1:
    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

    Method2:
    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
  • shiraj momin
    June 28, 2017 6:24 pm

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

    Reply
  • Dhiraj Lahane
    June 28, 2017 6:25 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
    GO

    Reply
  • Venkatasuryanarayana
    June 28, 2017 6:26 pm

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

    Reply

Leave a Reply