Solved

SQL ODD EVEN or NULL Coloum needed

Posted on 2013-01-29
7
333 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

861 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