Solved

SQL ODD EVEN or NULL Coloum needed

Posted on 2013-01-29
7
337 Views
Last Modified: 2013-01-29
Hi I have an SQL query and I want to add a coloum to it that shows if an employee is in the building.

At the moment Null is not in
Odd numbers are in and even they are out. Basicaly its how many times they have clocked in that day. This is what I want the table to look like after but at the moment only have the first 2 coloums.

Name          Count         InOrOut
name1             0             Day Off
name2             2                Out
name3             3                  In
name4             1                  In
name5             4                Out
0
Comment
Question by:taz8020
[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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38831882
>Null is not in.  Odd numbers are in and even they are out.
Use modulo to get the remainder of a divide by 2, then assign the label appropriately.

SELECT COALESCE(COUNT, 0) % 2 WHEN 0 THEN 'Out' WHEN 1 THEN 'In' END as InOrOut
0
 
LVL 11

Expert Comment

by:David Kroll
ID: 38831887
CASE
  WHEN (column IS NULL) OR ((column % 2) = 0)  THEN 'In'
  ELSE 'Out'
END as InOut
0
 
LVL 1

Expert Comment

by:DoutorApedeuta
ID: 38831899
Well, it would help if you showed the query that you have at the moment, but if I understand correctly, you could add the following column:

CASE WHEN COUNT(*) IS NULL THEN 'Out' WHEN COUNT(*) = 0 THEN 'Day Off' WHEN COUNT(*) % 2 = 0 THEN 'Out' ELSE 'In' END As InOrOut
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 38831902
<correction to my above SQL, but it looks like dkrollCTN has it too...>

SELECT CASE COALESCE(COUNT, 0) % 2 WHEN 0 THEN 'Out' WHEN 1 THEN 'In' END as InOrOut
0
 
LVL 3

Author Comment

by:taz8020
ID: 38831916
Sorry the coloum has got null in it, and this is the bit that I am stuck on

Name          Count         InOrOut
name1         NULL             Day Off
name2             2                Out
name3             3                  In
name4             1                  In
name5             4                Out
0
 
LVL 1

Accepted Solution

by:
DoutorApedeuta earned 500 total points
ID: 38831928
CASE WHEN Count IS NULL THEN 'Day Off' WHEN Count % 2 = 0 THEN 'Out' ELSE 'In' END As InOrOut
0
 
LVL 3

Author Closing Comment

by:taz8020
ID: 38831944
Your a star thanks for that, was driving me mad kept getting only 2 results on the 3 I wanted.
Thanks
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

696 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