?
Solved

SQL ODD EVEN or NULL Coloum needed

Posted on 2013-01-29
7
Medium Priority
?
350 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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 66

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
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 66

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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

840 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