SQL Server Admin
T-SQL Articles

May 08, 2010

SQL Query to find out who attended office on Saturday




A client of mine had a report requirement to find out the employees who attended office on Saturday in the last 30 days and their Check-In Check-Out times.

Here is some sample data that does that. For convenience, I have not added the CheckOut time, but hopefully you will get the idea

SAMPLE Data

DECLARE @TT TABLE (EID int, CheckIn DATETIME)
INSERT INTO @TT VALUES (2, 'April 02, 2010 11:25am')
INSERT INTO @TT VALUES (4, 'April 03, 2010 9:55am')
INSERT INTO @TT VALUES (5, 'April 07, 2010 11:24am')
INSERT INTO @TT VALUES (2, 'April 10, 2010 11:22am')
INSERT INTO @TT VALUES (3, 'April 15, 2010 5:27am')
INSERT INTO @TT VALUES (6, 'April 16, 2010 8:21am')
INSERT INTO @TT VALUES (7, 'April 17, 2010 11:55am')
INSERT INTO @TT VALUES (3, 'April 22, 2010 10:16am')
INSERT INTO @TT VALUES (4, 'April 24, 2010 11:35am')
INSERT INTO @TT VALUES (7, 'April 30, 2010 9:49pm')
INSERT INTO @TT VALUES (2, 'May 01, 2010 9:49am')
INSERT INTO @TT VALUES (5, 'May 02, 2010 10:43am')
INSERT INTO @TT VALUES (3, 'May 03, 2010 11:29am')

QUERY

SELECT * FROM @TT
WHERE DATENAME(weekday, CheckIn) = 'SATURDAY'
AND DATEDIFF(DD, CheckIn, GETDATE()) < 30

OUTPUT

Find Saturday

Hopefully these people get the bonus they deserve!


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter



 
  Feedback:

comments

3 Responses to "SQL Query to find out who attended office on Saturday"
  1. Shashi M said...
    May 8, 2010 7:11 AM

    Nice query! Can I group these dates together for eg- the ones that fall one after the other. An example would be

    April 2 to April 3
    April 7 to April 7
    April 15 to April 17

    and so on...

  2. Suprotim Agarwal said...
    May 9, 2010 9:28 PM

    Shashi: Yes you can. Infact I like your question. I will post the answer in the form of a post today. Hang on!

  3. Suprotim Agarwal said...
    May 10, 2010 5:04 AM

    Shashi: Here's your answer.

    Create Date Ranges for Consecutive dates in a SQL Server Table

    I hope this is what you were asking

 

Copyright © 2009-2011 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions