Solved

sql query SQL 2005

Posted on 2010-11-29
7
327 Views
Last Modified: 2012-05-10
HI,

I have a repor to fix and somebody has used this:

SELECT cast (case count(1) when 0 then 1 else 0 end as bit) ColumnA From TableX

totally intrigued why?

ColumnA returns either 0 or 1. Why count(1) is used and why 0 or 1?

Thanks
0
Comment
Question by:shmz
7 Comments
 
LVL 1

Expert Comment

by:hackpin
Comment Utility
well it the query uses count to check if there are records in table, you can use count(field name).
0
 
LVL 14

Expert Comment

by:Christopher Gordon
Comment Utility
heh, I agree strange syntax.  You usually see count(*) used to represent what the author is accomplishing with that query.  

All that is doing is counting the number of records in the result set.  You could really replace that with 0, or 1, or 2, or 'Eat at Joes' for that matter.

Count(*) is how i'd of wrote it instead of count(1).
0
 

Author Comment

by:shmz
Comment Utility
well, it seems that it turn to be 'true' or false when I run it in SQL server 2005 Report.

And then inside the report it checks to display a table in the report based on this value:

IIF (First(ColumnA.Value), "dataset1"), False, True)

I don't understand why if the result is 0 then 1 else 0....
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 500 total points
Comment Utility
SELECT cast (case count(1) when 0 then 1 else 0 end as bit) ColumnA From TableX

If TableX has data then return 0 (i.e when number of rows is 0 hen return true)

If TableX has no data return 1 (i.e. when number of rows is not 0 (then return false)



0
 
LVL 1

Expert Comment

by:_DJ
Comment Utility
count(1) Will return number of records selected from TableX.
CASE WHEN works as if else so if count(1) is 0 then it will return 1 else 0
and cast will convert it in true and false
0
 
LVL 7

Expert Comment

by:rmm2001
Comment Utility
Rewrite it..

SELECT cast (case count(*) when 0 then 1 else 0 end as bit) ColumnA From TableX

So SELECT COUNT(*) FROM Table. If COUNT(*) = 0 then return true (a 1). If its <> 0 then return false (a 0)

If the first value in ColumnA = "dataset1" then report back false. Else it's true.
0
 

Author Closing Comment

by:shmz
Comment Utility
Thanks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now