Solved

SQL ODD EVEN or NULL Coloum needed

Posted on 2013-01-29
7
314 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 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 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