August 25, 2015 at 9:57 am
We have bee using something like the following to get the Monday for the specified date.
However, we came across a scenario where this is not working
DECLARE @dt AS datetime
SET @dt = '2/19/2006'
SELECT DATEADD( ww, DATEDIFF( ww, 0, @dt),0)
It had seemed to be working as expected, but we recently came across a problem where The above is returning 2/20, not 2/13
August 25, 2015 at 9:59 am
blake.hartman (8/25/2015)
We have bee using something like the following to get the Monday for the specified date.However, we came across a scenario where this is not working
DECLARE @dt AS datetime
SET @dt = '2/19/2006'
SELECT DATEADD( ww, DATEDIFF( ww, 0, @dt),0)
It had seemed to be working as expected, but we recently came across a problem where The above is returning 2/20, not 2/13
Is this the only date? How are you using this at the moment?
August 25, 2015 at 10:06 am
We have been using that expression to get the "Week of" date based on the Monday of that week. For some reason though, it recently seems to be returning the following Monday date for Sundays
For example
DECLARE @dt AS datetime
SET @dt = '1/1/2015'
--SELECT DATEADD( ww, DATEDIFF( ww, 0, @dt),0)
SELECT
x.[Date]
,x.WeekOf
FROM
(
SELECT
@dt + t.n AS [Date]
,DATEADD( ww, DATEDIFF( ww, 0, @dt + t.n ),0) AS WeekOf
FROM
a1tbl_Tally t
WHERE
t.n < 8000
) AS x
WHERE
x.[Date] < x.WeekOf
August 25, 2015 at 10:07 am
Actually, this is the expected behavior.
What should it return for the Sunday date?
August 25, 2015 at 10:12 am
The previous Monday
So for Sunday Aug 23 2015, it should return Monday Aug 16 2015 instead of Monday Aug 24
August 25, 2015 at 10:14 am
Nevermind, I see the problem
It should be this
DATEADD( ww, DATEDIFF( ww, 0, <dateparam>-1 ), 0 )
I thought I was going crazy, but someone copied the wrong grouping expression and I missed the part where it should have had the -1 part
August 25, 2015 at 10:15 am
blake.hartman (8/25/2015)
Nevermind, I see the problemIt should be this
DATEADD( ww, DATEDIFF( ww, 0, <dateparam>-1 ), 0 )
I thought I was going crazy, but someone copied the wrong grouping expression and I missed the part where it should have had the -1 part
Actually, I'd do it like this:
SELECT DATEADD( ww, DATEDIFF( ww, 0, dateadd(day,-1,@dt)),0)
August 25, 2015 at 10:16 am
Why are you not using the DATEPART function to get the week number?
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
August 25, 2015 at 10:20 am
We need the actual date for the "week of" rather than the number and that seemed to be the simplest expression to calc that date
Is there an easier way to calc that using DATEPART?
August 25, 2015 at 10:21 am
Lynn Pettis (8/25/2015)
blake.hartman (8/25/2015)
Nevermind, I see the problemIt should be this
DATEADD( ww, DATEDIFF( ww, 0, <dateparam>-1 ), 0 )
I thought I was going crazy, but someone copied the wrong grouping expression and I missed the part where it should have had the -1 part
Actually, I'd do it like this:
SELECT DATEADD( ww, DATEDIFF( ww, 0, dateadd(day,-1,@dt)),0)
It makes sense consistency wise, it should really be written that way
August 25, 2015 at 10:25 am
blake.hartman (8/25/2015)
We need the actual date for the "week of" rather than the number and that seemed to be the simplest expression to calc that dateIs there an easier way to calc that using DATEPART?
Oops, I missed that.
I'd use my Dates dimension to lookup the desired date.
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
August 25, 2015 at 10:32 am
blake.hartman (8/25/2015)
Lynn Pettis (8/25/2015)
blake.hartman (8/25/2015)
Nevermind, I see the problemIt should be this
DATEADD( ww, DATEDIFF( ww, 0, <dateparam>-1 ), 0 )
I thought I was going crazy, but someone copied the wrong grouping expression and I missed the part where it should have had the -1 part
Actually, I'd do it like this:
SELECT DATEADD( ww, DATEDIFF( ww, 0, dateadd(day,-1,@dt)),0)
It makes sense consistency wise, it should really be written that way
Also, if @dt is declared as DATETIME2 or DATE for instance, this @dt - 1 will fail.
August 25, 2015 at 10:37 am
Try this formula:
--DATEADD( wk, DATEDIFF( dd, 0, @dt)/7,0)
SELECT dt, DATEADD( wk, DATEDIFF( dd, 0, dt)/7,0)
FROM (SELECT TOP 30
DATEADD( dd, ROW_NUMBER() OVER(ORDER BY @@SERVERNAME), '20150801')
FROM sys.all_columns) x(dt)
Viewing 13 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply