|
-
Dec 10th, 2010, 10:01 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] What is this SQL doing and - re-create in VB.Net
I need to recreate the following TSQL functionality in .Net, however I cant fugure out what it is doing within the nested parts.
I know that when run today it defaults to the 27th.
Code:
=DateAdd("d",28, DateAdd("d", -6,DateAdd("d", 1-Weekday(today),Today)))
-
Dec 10th, 2010, 11:24 AM
#2
Re: What is this SQL doing and - re-create in VB.Net
Best I can figure:
DateAdd("d",28, DateAdd("d", -6,DateAdd("d", 1-Weekday(today),Today)))
| 1 | DateAdd("d", 1-Weekday(today),Today)
1. TAKE 1 - the CURRENT DAY OF the week (Sunday = 0, Monday = 1,.... ) AND ADD that number OF DAYS TO todays date.
| 2 | DateAdd("d", -6,DateAdd("d", 1-Weekday(today),Today))
2. Substract six DAYS FROM the date caclulated IN part 1
| 3 | DateAdd("d",28, DateAdd("d", -6,DateAdd("d", 1-Weekday(today),Today)))
3 ADD 28 DAYS (basicly 4 weeks) TO the date caclulated IN part 2
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Dec 10th, 2010, 12:23 PM
#3
Thread Starter
Frenzied Member
Re: What is this SQL doing and - re-create in VB.Net
Based on the above I came up with the following code but unfortunately it comes out with the wrong date (11/01/2011)
Code:
DateTime defaultstart;
defaultstart = ((DateTime.Today.AddDays(DateTime.Today.Day)).AddDays(-6)).AddDays(28);
I am also a little confused about why take 6 off then 28, why not only take 22 off instead?
-
Dec 10th, 2010, 12:33 PM
#4
Re: What is this SQL doing and - re-create in VB.Net
It should be this:
Code:
DateTime defaultstart = DateTime.Today.AddDays((1 - DateTime.Today.DayOfWeek) - 6 + 28);
However, since you have the fixed values (-6) and (+28), you might just want to combine them and do
Code:
DateTime defaultstart = DateTime.Today.AddDays((1 - DateTime.Today.DayOfWeek) + 22);
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Dec 14th, 2010, 04:28 AM
#5
Thread Starter
Frenzied Member
Re: What is this SQL doing and - re-create in VB.Net
DateTime.Today.DayOfWeek of week returns "Tuesday" and cannot be implicitly cast as a double.
I also tried this
defaultstart = DateTime.Today.AddDays((1 - DateTime.Today.Day) + 22);
but this returns 23/12/2010 rather than the expected 03/01/2011
Apologies at this point, the original code is not direct TSQL it is infact a reporting services expression that I need to convert.
Last edited by FishGuy; Dec 14th, 2010 at 04:43 AM.
-
Dec 14th, 2010, 08:46 AM
#6
Re: What is this SQL doing and - re-create in VB.Net
Based on your 1st and last posts, you basically needs to calculate the date of the Monday 3 weeks away from this week's Monday. For example,
Code:
'1st post:
Today = 12/10/2010
This week's Monday = 12/6/2010
Calculated date = 12/27/2010 (3 weeks from this week's Moday)
'Last post:
Today = 12/14/2010
This week's Monday = 12/13/2010
Calculated date = 1/03/2011 (3 weeks from this week's Monday)
If this is the case then you simply do:
Code:
DateTime defaultstart = DateTime.Today.AddDays((1 - (int)DateTime.Today.DayOfWeek ) + 21);
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Dec 14th, 2010, 10:47 AM
#7
Thread Starter
Frenzied Member
Re: What is this SQL doing and - re-create in VB.Net
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|