March 13, 2009 at 4:19 am
I think this fulfills OP's amended requirements:
DECLARE @InputDate datetime
DECLARE @StartDate datetime
SELECT @InputDate = DATEADD(day, DATEDIFF(day, '17530101', '20090316 13:15'), '17530101')
SELECT @StartDate = DATEADD(DAY, DATEDIFF(day, '17530101', @InputDate) / 7 * 7, '17530101')
SELECT @InputDate AS InputDate,
@StartDate AS StartDate,
CASE WHEN (DATEDIFF(day, @StartDate, @InputDate) <= 4) THEN @InputDate
ELSE DATEADD(day, 4, @StartDate) END AS EndDate
July 5, 2017 at 3:44 am
Current Week ==>First day of the week :
SELECT CONVERT(VARCHAR(10),(DATEADD(DAY,-((DATEPART(DW, (DATEADD(WK, 0, GETDATE()))))-1),((DATEADD(WK,0,GETDATE()))))),120) AS [Current_Week_FirstDay]
Current Week ==> Last day of the week
SELECT CONVERT(VARCHAR(10),(DATEADD(DAY,7-((DATEPART(DW, (DATEADD(WK, 0, GETDATE()))))),((DATEADD(WK,0,GETDATE()))))),120) AS [Current_Week_LastDay]
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Last Week ==> First day of the week :
SELECT CONVERT(VARCHAR(10),(DATEADD(DAY,-((DATEPART(DW, (DATEADD(WK, -1, GETDATE()))))-1),((DATEADD(WK,-1,GETDATE()))))),120) AS [Last_Week_FirstDay]
Last Week ==> Last day of the week :
SELECT CONVERT(VARCHAR(10),(DATEADD(DAY,7-((DATEPART(DW, (DATEADD(WK, -1, GETDATE()))))),((DATEADD(WK,-1,GETDATE()))))),120) AS [Last_Week_LastDay]
July 5, 2017 at 6:55 am
karthik M - Tuesday, March 3, 2009 3:37 AMHi All,Thanks for all your inputs!:)I found the solution.Declare @InputDate Datetimeselect @InputDate = '02/mar/2009'select case when upper(datename(dw,@InputDate)) = 'MONDAY' then dateadd(dd,-7,@InputDate) when upper(datename(dw,@InputDate)) = 'TUESDAY' then dateadd(dd,-1,@InputDate) when upper(datename(dw,@InputDate)) = 'WEDNESDAY' then dateadd(dd,-2,@InputDate) when upper(datename(dw,@InputDate)) = 'THURSDAY' then dateadd(dd,-3,@InputDate) when upper(datename(dw,@InputDate)) = 'FRIDAY' then dateadd(dd,-4,@InputDate) when upper(datename(dw,@InputDate)) = 'SATURDAY' then dateadd(dd,-5,@InputDate) when upper(datename(dw,@InputDate)) = 'SUNDAY' then dateadd(dd,-6,@InputDate) END as StartDate, case when upper(datename(dw,@InputDate)) = 'MONDAY' then dateadd(dd,-3,@InputDate) when upper(datename(dw,@InputDate)) in('TUESDAY','WEDNESDAY','THURSDAY','FRIDAY') then @InputDate when upper(datename(dw,@InputDate)) = 'SATURDAY' then dateadd(dd,-1,@InputDate) when upper(datename(dw,@InputDate)) = 'SUNDAY' then dateadd(dd,-2,@InputDate) END as EndDate It worked well for all the cases.
Such complexity is not necessary. Please tell us what day of the week a week starts with.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 3 posts - 46 through 47 (of 47 total)
You must be logged in to reply to this topic. Login to reply