sqldba2013
asked on
Microsoft sql query error
Hi Experts,
I am not able to execute attached query because of below error. Could you please advise how to fix below error.
Error:
Msg 8117, Level 16, State 1, Line 1
Operand data type bit is invalid for sum operator.
I am not able to execute attached query because of below error. Could you please advise how to fix below error.
Error:
Msg 8117, Level 16, State 1, Line 1
Operand data type bit is invalid for sum operator.
select CAST(CAST(SUM([Del]) AS DECIMAL) / COUNT([Del]) AS DECIMAL(36, 4)) AS [Del_%]
from dbo.TEMP_vw
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have tried with above query and still I'm getting same error.
Msg 8117, Level 16, State 1, Line 1
Operand data type bit is invalid for sum operator.
Msg 8117, Level 16, State 1, Line 1
Operand data type bit is invalid for sum operator.
can you post few rows from this table ?
Less code a without CASE keyword^:
select SUM(CAST(Del AS DECIMAL(36,4)))/ COUNT(Del) [Del_%]
from dbo.TEMP_vw
ASKER
Thanks Vitor Montalvão and Vishal Patil for your help. Now I am able to execute above query without error.
I am marking this case as closed.
I am marking this case as closed.
If your column type is bit then you should check for the values 'True' or 'False'. Bit column wont return you the values 1 or 0 directly.
Please try following query. If this wont work then we need to check the structure of your table.
Please try following query. If this wont work then we need to check the structure of your table.
select CAST(CAST(SUM(case [Del] when 'True' then 1 else 0 end) AS DECIMAL) / COUNT([Del]) AS DECIMAL(36, 4)) AS [Del_%]
from dbo.TEMP_vw
Forgot to post the MSDN article
Open in new window