SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Newbie needs help


Newbie needs help

Author
Message
krzykris
krzykris
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 10
Hi,

Newbie here would like some help if possible. I’m trying to write a sql statement to extract “Previous” and “Current Week” from attendance table. The field for date is EnrollmentDate([highlight=#ffff11]datetimeoffset[/highlight](7), null) also our weeks are from Sunday through Saturday. I tried writing the statement belown but can’t get the correct Sunday through Saturday week for both “Previous” and “Current Week”. I believe it's because field is datetimeoffset??? Any and all help is most definitely appreciated.


“Current Week”
“I GET June 17 – 22 but not Sunday June 16”

Select * from dbo.Enrollment
Where enrollmentdate between DATEADD(wk,DATEDIFF(wk,0,GETDATE()), 0) and DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 6)


“Previous Week”
“I GET June 9 – 14 but not Saturday June 15”

Select * from dbo.Enrollment
Where enrollmentdate BETWEEN DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0) AND DATEADD(wk,DATEDIFF(wk,7,GETDATE()),6)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59556 Visits: 17947
See if this article helps point you in the right direction.

http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Jan Van der Eecken
Jan Van der Eecken
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3197 Visits: 6497
Your problem is that you are adding the number of weeks to the base date of zero (1 January 1900), which happened to be a Monday. Changing your queries to something like

DATEADD ( wk, DATEDIFF ( wk, 0, GETDATE() ), '1899-12-31' )




will give you the correct result if you want the week to start on Sunday.

Regards,
Jan

--------------------------------------------------------------------------
A little knowledge is a dangerous thing (Alexander Pope)

In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)
krzykris
krzykris
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 10
Thank you for the help.
Jan Van der Eecken
Jan Van der Eecken
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3197 Visits: 6497
No problem, that's what we are here for.

--------------------------------------------------------------------------
A little knowledge is a dangerous thing (Alexander Pope)

In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search