Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sgandhi
Posting Yak Master
115 Posts |
Posted - 2009-09-04 : 10:43:35
|
Hi, I need to write a expression in SSRS in the FROM Date to be the last sunday.EG: If todays date is 9/4/2009 the in the FROM Date i want it to be 8/30/2009If todays date is 8/19/2009 then the FROMDATE would be 8/16/2009I dont know how to write this expression. Can someone help |
|
sinclair
Starting Member
7 Posts |
Posted - 2009-09-07 : 04:43:52
|
Below is a function I got a while ago from somewhere. create function [dbo].[F_START_OF_WEEK]( @DATE datetime, -- Sun = 1, Mon = 2, Tue = 3, Wed = 4 -- Thu = 5, Fri = 6, Sat = 7 -- Default to Sunday @WEEK_START_DAY int = 1 )/*Find the first date on or before @DATE that matches day of week of @WEEK_START_DAY.*/returns datetimeasbegindeclare @START_OF_WEEK_DATE datetimedeclare @FIRST_BOW datetime-- Check for valid day of weekif @WEEK_START_DAY between 1 and 7 begin -- Find first day on or after 1753/1/1 (-53690) -- matching day of week of @WEEK_START_DAY -- 1753/1/1 is earliest possible SQL Server date. select @FIRST_BOW = convert(datetime,-53690+((@WEEK_START_DAY+5)%7)) -- Verify beginning of week not before 1753/1/1 if @DATE >= @FIRST_BOW begin select @START_OF_WEEK_DATE = dateadd(dd,(datediff(dd,@FIRST_BOW,@DATE)/7)*7,@FIRST_BOW) end endreturn @START_OF_WEEK_DATEendGO |
 |
|
|
|
|