Dears,
I am facing one issue in my below query trying to do the calculation and pass the value in new column but getting like below.
My Query
CASE WHEN '' = '' THEN credit_amount * 0.0299 ELSE '0.00' END AS TexValue,
CASE WHEN '' = '' THEN credit_amount / 1.15 ELSE '0.00' END AS TXTVAT,
Error
Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to numeric.
Completion time: 2024-01-21T09:47:02.4563341+03:00

Jignesh KumarRank #91 · 22.9K reputationPosted Jan 21, 2024, 1:55 AM EST
Hello Feroz,
Your CASE WHEN statement is wrong, you need to add a column to check the condition
CASE WHEN credit_amount != '' THEN credit_amount * 0.0299 ELSE '0.00' END AS TexValue,Example, please refer below query and change according to your requirement
SELECT OrderID, Quantity, CASE WHEN Quantity > 30 THEN 'The quantity is greater than 30' WHEN Quantity = 30 THEN 'The quantity is 30' ELSE 'The quantity is under 30' END AS QuantityText FROM OrderDetails;Feroz KhanRank #3,748 · 395 reputationPosted Jan 21, 2024, 3:03 AM EST
Dear Jignesh,
Thank you for your reply i tried as per you query but still i am getting error i tried to convert nvarchar to number still same.
CASE WHEN credit_amount != '' THEN CONVERT(decimal(7,2), credit_amount * 0.0299)
WHEN credit_amount = '' THEN CONVERT(decimal(7,2), credit_amount) ELSE '0.00' END AS TexValue,
Error
Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to numeric.
Completion time: 2024-01-21T11:02:04.2879684+03:00
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 21, 2024, 3:19 AM EST
Ok, If you are converting varchar to decimal then need to check all values must be convertible to decimal, you should check for null and "" values as well,
First of all
declare @credit_amount varchar(10)= '100' select CASE WHEN isnull(@credit_amount,0) != '' THEN CONVERT(varchar(10), CONVERT(decimal(7,2), isnull(@credit_amount,0)) * 0.0299) ELSE '0.00' END AS TexValueJithu ThomasRank #437 · 5.7K reputationPosted Jan 21, 2024, 1:52 AM EST
It looks like you are trying to conditionally calculate values based on whether a condition is met in your SQL query. However, there are a couple of issues in your provided SQL snippet.
The condition
'' = ''will always evaluate totrue, so theELSEpart of yourCASEstatement will never be executed.In your
THENandELSEclauses, you are using string literals ('0.00') instead of numerical values. If you want to perform mathematical operations, you should use numeric values without quotes.Here's a corrected version of your query:
CASE WHEN
CASE WHEN
Please accept the answer if you find the same is useful!