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 KumarPosted Jan 21, 2024, 8:19 AM
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
Feroz KhanPosted Jan 21, 2024, 8:03 AM
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 KumarPosted Jan 21, 2024, 6:55 AM
Hello Feroz,
Your CASE WHEN statement is wrong, you need to add a column to check the condition
Example, please refer below query and change according to your requirement
Jithu ThomasPosted Jan 21, 2024, 6:52 AM
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!