December 16, 2009 at 8:01 am
Hi,
I have a date that's in a variable and I have a data transformation task that's going to create a new column called as_of_date. I can't figure out how i would get the date for the friday before the date that's in the variable. The variable is just called "time". Can anyone help?
December 16, 2009 at 8:27 am
Here's how you'd do it in T-SQL:
declare @Date datetime;
select @Date = getdate();
select dateadd(week, datediff(week, '12/31/1999', @Date)-1, '12/31/1999');
I picked 31 Dec '99 because that was a Friday. So it finds the number of weeks since a known Friday, and then adds that number of weeks to that Friday. That gives the prior Friday.
Note that if you assign a value to @Date that is a Friday, it will give the prior Friday.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply