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
Solved

sql query SQL 2005

Posted on 2010-11-29
7
331 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query to return total 6 22
MySQL Memory Keeps Increasing 4 47
date diff with Fiscal Calendar 4 55
Use Select Query to Return Results as a Form 9 24
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Copy Database Wizard 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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

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