使用MySql获取特定日期
问题描述:
当需要输入B的值时,我需要在一个月内获得第B个工作日。 例如,如果2013年1月份的b = 12,则结果值应该采用日期格式为'17 -01-2013',因为结果在排除周六,周日&当月假期后计算结果为 。使用MySql获取特定日期
我已经用下面的代码&试图在它的SQLserver工作正常,但是我发现很难在MySQL中执行它的某些功能不可用 作为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
注
假期是静态的假期表,包含2013
假期表,我需要在MySQL类似的实例。 请帮助我。
答
如果您需要在末端偏移0的第一天集合。如果第二OFFSET 1中,如果15个集偏移14
select d
FROM
(
SELECT @row := @row + 1 as row,
DATE_ADD('2013-01-01', INTERVAL @row-1 DAY) d
from
(SELECT @row := 0) r,
(
select 1 n
union all
select 2 n
union all
select 3 n
union all
select 4 n
union all
select 5 n
union all
select 6 n
) t1,
(
select 1 n
union all
select 2 n
union all
select 3 n
union all
select 4 n
union all
select 5 n
union all
select 6 n
) t2
) num_seq
where
d<DATE_ADD('2013-01-01', INTERVAL 1 MONTH)
and d not in (select hdate from Holidays)
and DAYNAME(d) not in ('Saturday','Sunday')
order by d
LIMIT 1 OFFSET 20
版无偏移和LIMIT。请参阅最新的where r=1
这是第一天。如果您需要15个天更改为where r=15
select d
from
(
select d,@r := @r + 1 as r
FROM
(SELECT @r := 0) r1,
(
SELECT @row := @row + 1 as row,
DATE_ADD('2013-01-01', INTERVAL @row-1 DAY) d
from
(SELECT @row := 0) r,
(
select 1 n
union all
select 2 n
union all
select 3 n
union all
select 4 n
union all
select 5 n
union all
select 6 n
) t1,
(
select 1 n
union all
select 2 n
union all
select 3 n
union all
select 4 n
union all
select 5 n
union all
select 6 n
) t2
) num_seq
where
d<DATE_ADD('2013-01-01', INTERVAL 1 MONTH)
and d not in (select hdate from Holidays)
and DAYNAME(d) not in ('Saturday','Sunday')
order by d
) rTable
where r=1
答
如何得到相同的结果时,只月份和年份作为参数传递。因为当我检查代码时......它的工作时间是相应月份的第一天,就像我输入参数为'2013-01-01'一样,结果是绝对的,但如果日期为'2013-01 -15'程序计算第1天的第15天并计算从那里开始的第n天。
+0
Valex你可以看看这种情况。 – Bobby
对于MYSQL,您可以使用[dayname](http://www.w3resource.com/mysql/date-and-time-functions/mysql-dayname-function.php)来替换'datename'。 (日期,日期,日期,@ fromDate))) order by(dateadd(day,DayNo,@ fromDate)))as Business_day' – bonCodigo
雅。我们也处于同样的境地。 – Bobby
对于问题+1,如果所有查询都不在ANSI中,并且某些功能在新平台中不可用,则迁移可能会很痛苦;;) – bonCodigo