April 30, 2015 at 2:49 pm
I need to divide a number by the number of full weeks based on today's date.
Say today is April 30 which is Week 18th but since its the middle of the week i would like to get 17. IS that possible?
April 30, 2015 at 3:09 pm
full weeks since when? the first of the year?
Maybe Lynn Pettis' article will help:
http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/
April 30, 2015 at 3:20 pm
That helps..Thanks a lot
April 30, 2015 at 3:40 pm
sharonsql2013 (4/30/2015)
That helps..Thanks a lot
Actually, it doesn't. The WEEK datepart doesn't count whole weeks.
--===== Note that the dates below occur on an adjacent Wednesday and Thursday respectively.
SELECT DATEPART(WEEK,'2014-12-31'), DATEPART(WEEK,'2015-01-01');
You also haven't defined when a week starts nor when a year starts and it can easily be different from instance to instance.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 30, 2015 at 4:09 pm
I want the start of the week from 04-12-2015 thru 4-18-2015. (Sat thru Sun)
April 30, 2015 at 5:26 pm
What if you use a Calendar table?
April 30, 2015 at 8:17 pm
sharonsql2013 (4/30/2015)
I want the start of the week from 04-12-2015 thru 4-18-2015. (Sat thru Sun)
What should the start of the week for the 1st of January 2015 be and should it be considered as 2014 or 2015? Also, I think you meant Sunday through Saturday? If that's true, then having a value of DATEFIRST =7 for the server and using Bill's WEEK calculations will work fine.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 30, 2015 at 10:39 pm
sharonsql2013 (4/30/2015)
I need to divide a number by the number of full weeks based on today's date.Say today is April 30 which is Week 18th but since its the middle of the week i would like to get 17. IS that possible?
(Almost) everything is possible, consider this code, should be enough to get you passed this hurdle.
😎
USE tempdb;
GO
SET NOCOUNT ON;
DECLARE @MY_DATE DATE = CONVERT(DATE,'20150430',112);
DECLARE @ZERO_DATE DATE = CONVERT(DATE,'19000101',112);
SELECT
/* Day of the week, 1 = Monday*/
(DATEDIFF(DAY,@ZERO_DATE,@MY_DATE) % 7) + 1
/* Number of weeks since Monday 1900-01-01 */
,DATEDIFF(DAY,@ZERO_DATE,@MY_DATE) / 7.0
/* First day of this year */
,DATEADD(YEAR,DATEDIFF(YEAR,@ZERO_DATE,@MY_DATE),@ZERO_DATE)
/* Number of days since Monday 1900-01-01 until the beginning of this year */
,DATEDIFF(DAY,@ZERO_DATE,DATEADD(YEAR,DATEDIFF(YEAR,@ZERO_DATE,@MY_DATE),@ZERO_DATE)) / 7.0
/* Day of the week at the beginning of the year, 1 = Monday*/
,(DATEDIFF(DAY,@ZERO_DATE,DATEADD(YEAR,DATEDIFF(YEAR,@ZERO_DATE,@MY_DATE),@ZERO_DATE)) % 7) + 1
/* Days passed since beginning of the year */
,(DATEDIFF(DAY,@ZERO_DATE,@MY_DATE) / 7.0)
- (DATEDIFF(DAY,@ZERO_DATE,DATEADD(YEAR,DATEDIFF(YEAR,@ZERO_DATE,@MY_DATE),@ZERO_DATE)) / 7.0)
/* Day of the year */
,DATEPART(DAYOFYEAR,@MY_DATE)
/* Week of the year */
,DATEPART(WEEK,@MY_DATE)
/* ISO_WEEK of the year */
,DATEPART(ISO_WEEK,@MY_DATE)
May 1, 2015 at 7:10 am
Eirikur Eiriksson (4/30/2015)
sharonsql2013 (4/30/2015)
I need to divide a number by the number of full weeks based on today's date.Say today is April 30 which is Week 18th but since its the middle of the week i would like to get 17. IS that possible?
(Almost) everything is possible, consider this code, should be enough to get you passed this hurdle.
😎
USE tempdb;
GO
SET NOCOUNT ON;
DECLARE @MY_DATE DATE = CONVERT(DATE,'20150430',112);
DECLARE @ZERO_DATE DATE = CONVERT(DATE,'19000101',112);
SELECT
/* Day of the week, 1 = Monday*/
(DATEDIFF(DAY,@ZERO_DATE,@MY_DATE) % 7) + 1
/* Number of weeks since Monday 1900-01-01 */
,DATEDIFF(DAY,@ZERO_DATE,@MY_DATE) / 7.0
/* First day of this year */
,DATEADD(YEAR,DATEDIFF(YEAR,@ZERO_DATE,@MY_DATE),@ZERO_DATE)
/* Number of days since Monday 1900-01-01 until the beginning of this year */
,DATEDIFF(DAY,@ZERO_DATE,DATEADD(YEAR,DATEDIFF(YEAR,@ZERO_DATE,@MY_DATE),@ZERO_DATE)) / 7.0
/* Day of the week at the beginning of the year, 1 = Monday*/
,(DATEDIFF(DAY,@ZERO_DATE,DATEADD(YEAR,DATEDIFF(YEAR,@ZERO_DATE,@MY_DATE),@ZERO_DATE)) % 7) + 1
/* Days passed since beginning of the year */
,(DATEDIFF(DAY,@ZERO_DATE,@MY_DATE) / 7.0)
- (DATEDIFF(DAY,@ZERO_DATE,DATEADD(YEAR,DATEDIFF(YEAR,@ZERO_DATE,@MY_DATE),@ZERO_DATE)) / 7.0)
/* Day of the year */
,DATEPART(DAYOFYEAR,@MY_DATE)
/* Week of the year */
,DATEPART(WEEK,@MY_DATE)
/* ISO_WEEK of the year */
,DATEPART(ISO_WEEK,@MY_DATE)
That's all true but, except for ISO_WEEK, I'd like to know what the OP wants done for the first and last weeks of any given year, which are partial weeks 6 out of 7 times.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply