Solved

sql query SQL 2005

Posted on 2010-11-29
7
332 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
ID: 34235501
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
ID: 34235538
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
ID: 34235570
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 500 total points
ID: 34235593
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
ID: 34236556
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
ID: 34241649
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
ID: 34253947
Thanks
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.

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