Solved

Microsoft sql query error

Posted on 2014-09-23
10
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 49

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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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 49

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 49

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 49

Expert Comment

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

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL 2014 missing dll from Bin? 3 34
Finding Where Clause Value in SQL Views and SP 21 41
Access #Deleted data 20 43
Database-Scoped Permissions 2 17
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

733 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