Hi
My problem is second thusday of month how to calculate in sql server
like 2nd thusday .
08/01/2015
12/02/2015
12/03/2015
09/04/2015
14/05/2015
10/06/2015
09/07/2015
13/08/2015
10/09/2015
08/10/2015
12/11/2015
10/12/2015
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Surendra KandiraPosted Feb 20, 2016, 7:31 AM
Upendra Pratap ShahiPosted Jan 6, 2016, 8:12 AM
Surendra KandiraPosted Dec 28, 2015, 6:57 AM
(
@theDate DATETIME,
@theWeekday TINYINT,
@theNth SMALLINT
)
RETURNS DATETIME
BEGIN
RETURN (
SELECT theDate
FROM (
SELECT DATEADD(DAY, 7 * @theNth - 7 * SIGN(SIGN(@theNth) + 1) +(@theWeekday + 6 - DATEDIFF(DAY, '17530101', DATEADD(MONTH, DATEDIFF(MONTH, @theNth, @theDate), '19000101')) % 7) % 7, DATEADD(MONTH, DATEDIFF(MONTH, @theNth, @theDate), '19000101')) AS theDate
WHERE @theWeekday BETWEEN 1 AND 7
AND @theNth IN (-5, -4, -3, -2, -1, 1, 2, 3, 4, 5)
) AS d
WHERE DATEDIFF(MONTH, theDate, @theDate) = 0
)
END
Upendra Pratap ShahiPosted Nov 6, 2015, 12:00 PM
Upendra Pratap ShahiPosted Nov 5, 2015, 12:30 PM
This return for current month-
SELECT
CONVERT(CHAR(10),
DATEADD(wk,1,(CURRENT_TIMESTAMP -
Day(CURRENT_TIMESTAMP)+1)+(5-DATEPART(dw,(CURRENT_TIMESTAMP -
Day(CURRENT_TIMESTAMP)+1)))),
121)
Upendra Pratap ShahiPosted Nov 5, 2015, 12:27 PM
Hello,
( monthdate date not null primary key )
insert
into #monthdates ( monthdate )
values
( '2015-11-05' )
, ( '2014-11-06' )
, ( '2013-11-07' )
select monthdate as first_thursday_of_month
, dateadd(day
, ( ( 17 - datepart(dw,monthdate) ) % 7 ) + 7
, monthdate
) as second_thursday_of_month
from #monthdates
drop table #monthdates
Surendra KandiraPosted Nov 5, 2015, 8:15 AM
select @yearNo=cast(datepart(yy,GETDATE()) as char(4))
select @weekNo={fn week(GETDATE())}
select @weekStart=dateadd(wk,Datediff(wk,6,'1/1/'+@yearNo)+(@weekNo-1),6)
select @weekEnd=dateAdd(wk,datediff(wk,5,'1/1/'+@yearNo)+(@weekNo-1),5)
print @weekStart
print @weekEnd