Solved

Microsoft sql query error

Posted on 2014-09-23
10
361 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 47

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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 47

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 47

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 47

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

809 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