Solved

Microsoft sql query error

Posted on 2014-09-23
10
364 Views
Last Modified: 2014-09-23
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.
select CAST(CAST(SUM([Del]) AS DECIMAL) / COUNT([Del]) AS DECIMAL(36, 4)) AS [Del_%]
from dbo.TEMP_vw

Open in new window

0
Comment
Question by:sqldba2013
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40338692
try this

select CAST((CAST(SUM([Del]) AS DECIMAL) / COUNT([Del])) AS DECIMAL(36, 4)) AS [Del_%]
from dbo.TEMP_vw

Open in new window

0
 
LVL 48

Accepted Solution

by:
Vitor Montalvão earned 250 total points
ID: 40338701
If Del is a bit column then you can't use SUM directly on this field. You'll need something like this:
select CAST(SUM(CASE Del
		WHEN 1 THEN 1
		ELSE 0
		END) / COUNT(Del) AS  DECIMAL(36, 4)) [Del_%]
from dbo.TEMP_vw

Open in new window

0
 
LVL 2

Assisted Solution

by:Vishal Patil
Vishal Patil earned 250 total points
ID: 40338705
Hello sqldba2013,

If you are using bit column you can not directly apply Sum operation on it. Try with following query.

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

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:sqldba2013
ID: 40338709
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.
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40338711
can you post few rows from this table ?
0
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40338715
Less code a without  CASE keyword^:
select SUM(CAST(Del AS DECIMAL(36,4)))/ COUNT(Del) [Del_%]
from dbo.TEMP_vw

Open in new window

0
 

Author Closing Comment

by:sqldba2013
ID: 40338718
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.
0
 
LVL 2

Expert Comment

by:Vishal Patil
ID: 40338723
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.

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

Open in new window

0
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40338814
@Vishal

Bit datatype means binary values, so 0 and 1 should be the accepted values.
Boolean datatype accepts TRUE and FALSE but SQL Server doesn't have a Boolean datatype. And as you can see you need to write 'True' and 'False' as VARCHAR so SQL Server can convert it to bit.
0
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40338817
Forgot to post the MSDN article
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question