Solved

Query  - show records where a date field is this week

Posted on 2015-02-03
9
95 Views
Last Modified: 2015-02-10
I need to query to return all records where [completed date] was this week...

Also, i need to do the same for month
0
Comment
Question by:vbnetcoder
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40586954
This month:

WHERE [completed date] >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AND
    [completed date] < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0)

This week is very similar but depends on how you define a week - what day does your week start on?
0
 

Author Comment

by:vbnetcoder
ID: 40587059
I think week is going to be Sunday --> Saturday ...ie just a normal week
0
 

Author Comment

by:vbnetcoder
ID: 40587092
could you give me that query too so i don't mess it up?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40587103
WHERE [completed date] >= DATEADD(DAY, -DATEDIFF(DAY, 6, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)) % 7,DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0))
    AND [completed date] < DATEADD(DAY, -DATEDIFF(DAY, 6, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)) % 7 + 7,DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0))

You can simplify it if you add a variable for the current day with time stripped.


DECLARE @today_midnight datetime
SET @today_midnight = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)

WHERE [completed date] >= DATEADD(DAY, -DATEDIFF(DAY, 6, @today_midnight) % 7, @today_midnight) AND
    [completed date] < DATEADD(DAY, -DATEDIFF(DAY, 6, @today_midnight) % 7 + 7, @today_midnight)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40587119
Similarly, for the month:

DECLARE @start_of_month_midnight
SET @start_of_month_midnight = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)

...
WHERE [completed date] >= @start_of_month_midnight AND
     [completed date] < DATEADD(MONTH, 1, @start_of_month_midnight)
0
 
LVL 49

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40588099
If you don't have any index on the [completed date] column you can use the DATEPART function. It's more easy for read:
-- Select records within same week
SELECT *
FROM YourTableName
WHERE DATEPART(week, [completed date]) = DATEPART(week, GETDATE())

-- Select records within same month
SELECT *
FROM YourTableName
WHERE DATEPART(month, [completed date]) = DATEPART(month, GETDATE())

Open in new window


NOTE: You need to check what's the weekday set as first day of week. For that run this command: SELECT @@DATEFIRST (1-Monday, ..., 7-Sunday)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40588781
>> If you don't have any index on the [completed date] column <<

But an index could be added next week.  It's very poor practice to deliberately exclude indexes from consideration, particularly due to lazy coding.

You really should always avoid using functions on table columns in a WHERE or JOIN whenever possible.  One, ISNULL, should never be used in a WHERE or JOIN, because it can always be easily coded around.

But, if you do use the month,  you need to include a year check for accuracy, or you'd get every year's rows from the table.
0
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 40588858
But, if you do use the month,  you need to include a year check for accuracy, or you'd get every year's rows from the table.
Yes, that's true. I've missed the year in my suggested solution.
0
 

Author Closing Comment

by:vbnetcoder
ID: 40600529
ty
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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…

749 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