Solved

Query  - show records where a date field is this week

Posted on 2015-02-03
9
88 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
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
 
LVL 69

Expert Comment

by:ScottPletcher
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
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 69

Expert Comment

by:ScottPletcher
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 45

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:ScottPletcher
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 45

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Row-Level Security 2 20
Sql query 34 22
Slow SQL query 12 27
Minus first query 1 0
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

747 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

10 Experts available now in Live!

Get 1:1 Help Now