I need to get Bth working day in a month when the value of B is entered.
For example, If b=12 in the month of January,2013 the resultant value should be in the date format as ’17-01-2013′ as the result is calculated
after excluding Saturdays, Sundays & holidays in the month.
I have tried it in SQLserver with the following code & its working fine, but Im finding it difficult to execute it in MySql as some functions are not
available as in Sqlserver.
Declare
@fromDate Date,
@Daydiff int
Set @fromDate ='01 jan 2013'
Set @Daydiff=datediff(day, @fromdate, dateadd(month, 1, @fromdate))
Select * from
(
Select
dateadd(day,DayNo,@fromDate) as Date,
dateName(weekday,(dateadd(day,DayNo,@fromDate))) As WeekDate,
Datename(month,(dateadd(day,DayNo,@fromDate))) as MonthName,
Row_number() Over (partition by (DatePart(month,(dateadd(day,DayNo,@fromDate))))
order by (dateadd(day,DayNo,@fromDate))) as Business_day
from
(Select top (@Daydiff) row_number() over(order by (select 1))-1 as DayNo
from sys.syscolumns a cross join sys.syscolumns b)Dates
Where
dateName(weekday,(dateadd(day,DayNo,@fromDate))) Not In ('Saturday','Sunday') and
dateadd(day,DayNo,@fromDate) Not In (Select hdate from Holidays)
)A
Where Business_day=1
Note
Holidays is the static holidays table which contains list of holidays of 2013
I need a similar instance in Mysql.
Kindly help me with this.
SQLFiddle demo
If you need first day set OFFSET 0 in the end. If the second OFFSET 1, if 15-th set OFFSET 14
Version without OFFSET and LIMIT. See the latest
where r=1it is the 1-st day. If you need 15-th day change towhere r=15SQLFiddle demo