我将日期时间存储在我的表中的 LeaveFrom 和 LeaveTo 字段中的叶子,例如
LeaveFrom = 05/26/2012 12:00:00 和 LeaveTo = 06/30/2012.. 等等
从前端我只传递月份和年份。那么相同的搜索条件是什么。
我需要按月和按年进行搜索的标准。在特定月份有多少人申请休假/或缺勤。
感谢你们所有人……几乎所有人都接近预期……谢谢,我需要的是“提示”……非常感谢你们所有人……
我将日期时间存储在我的表中的 LeaveFrom 和 LeaveTo 字段中的叶子,例如
LeaveFrom = 05/26/2012 12:00:00 和 LeaveTo = 06/30/2012.. 等等
从前端我只传递月份和年份。那么相同的搜索条件是什么。
我需要按月和按年进行搜索的标准。在特定月份有多少人申请休假/或缺勤。
感谢你们所有人……几乎所有人都接近预期……谢谢,我需要的是“提示”……非常感谢你们所有人……
假设您想获得一组在给定月/年休假的人:
以下是一些可能有用的完整更新代码:
declare @dateFrom datetime
declare @dateTo datetime
set @dateFrom = DateAdd(day, 0, DateAdd(month, @Month - 1,
DateAdd(Year, @Year-1900, 0)))
set @dateTo = DATEADD("month", 1, @dateFrom)
SELECT * FROM @yourtable WHERE LeaveFrom < @dateTo AND LeaveTo >= @dateFrom
我只计算一次日期,而不是为每个选定的行计算日期。上面的代码也可以很容易地转换成一条 SQL 语句。我这样写是为了使其易于阅读。
试试这个:
where (MONTH(LeaveFrom) = @Month
and YEAR(LeaveFrom) = @Year )
OR
(and MONTH(LeaveTo) = @Month
and YEAR(LeaveTo) = @Year )
SELECT * FROM {your table name}
WHERE LeaveFrom >= CAST({input month} + '/1/' + {input year} AS DATETIME) AND
LeaveTo <= DATEADD(dd, -1, DATEADD(mm, 1, CAST({input month} + '/1/' + {input year} AS DATETIME)))
更准确地说,你可以减去一分钟而不是一整天。
SELECT * FROM {your table name}
WHERE LeaveFrom >= CAST({input month} + '/1/' + {input year} AS DATETIME) AND
LeaveTo < DATEADD(mm, 1, CAST({input month} + '/1/' + {input year} AS DATETIME))
你开始让每个人都对你想要的东西感到困惑,但根据你最后的评论,这里是查询。
SELECT * FROM {your table name}
WHERE MONTH(LeaveFrom) = {input month} AND YEAR(LeaveFrom) = {input year}
DECLARE @month int
DECLARE @year int
SET @month = 7
SET @year = 2012
SELECT * FROM table
WHERE
(DATEPART(yy, LeaveFrom) = @year
AND DATEPART(mm, LeaveFrom) = @month) OR
(DATEPART(mm, LeaveTo) = @month AND DATEPART(yy, LeaveTo) = @Year)
据我所知,您希望所有在 LeaveFrom 和 LeaveTo 之间与所选月份重叠的行。我提供了一些示例日期作为文档。
declare @yourtable table(LeaveFrom datetime, LeaveTo datetime)
insert @yourtable values('2012-05-26 12:00:00', '2012-06-30')
insert @yourtable values('2012-01-26 12:00:00', '2012-12-30')
insert @yourtable values('2012-01-26 12:00:00', '2012-04-30')
insert @yourtable values('2012-05-26 12:00:00', '2012-12-30')
insert @yourtable values('2012-01-26 12:00:00', '2012-05-30')
insert @yourtable values('2012-06-26 12:00:00', '2012-12-30')
insert @yourtable values('2011-01-01 12:00:00', '2011-01-01')
declare @month tinyint = 5
declare @year int = 2012
-- calculate a data corresponding to month and year (2012-05-01)
declare @date datetime= dateadd(month, (@year-1900) * 12 + @month-1, 0)
select * from @yourtable
where datediff(month , LeaveFrom, @date) between 0 and datediff(month , LeaveFrom, LeaveTo)
编辑:具有相同结果的另一种可能方式:
select * from @yourtable
where @date between dateadd(month, datediff(month, 0, LeaveFrom), 0)
and dateadd(month, datediff(month, 0, LeaveTo), 0)
结果:
LeaveFrom LeaveTo
----------------------- -----------------------
2012-05-26 12:00:00.000 2012-06-30 00:00:00.000
2012-01-26 12:00:00.000 2012-12-30 00:00:00.000
2012-05-26 12:00:00.000 2012-12-30 00:00:00.000
2012-01-26 12:00:00.000 2012-05-30 00:00:00.000
试试这个。性能方面,这将做得最好
declare @month int, @year int
select @month=6, @year=2012
select columns from table
where
LeaveFrom>=Dateadd(month,(@year-1900)*12+@month-1,0) and
LeaveTo<Dateadd(month,(@year-1900)*12+@month,0)
您可能还需要知道如何以各种方式模拟日期序列函数 http://beyondrelational.com/modules/2/blogs/70/posts/15788/10-ways-to-simulate-dateserial-function.aspx
DECLARE @Temp TABLE ( ID INT IDENTITY(1, 1) ,LeaveFrom DATETIME ,LeaveTo DATETIME ,UserName VARCHAR(100) )
插入@Temp值('2015-01-01','2015-01-05','Ramesh'),('2015-01-07','2015-01-09','Suresh'),( '2015-01-03','2015-01-06','Dinesh'),('2015-01-15','2015-01-25','Kamlesh'),('2015-01-12 ' ,'2015-01-17' ,'莫寒' )
SELECT COUNT(*) FROM @Temp WHERE '2015' BETWEEN YEAR(LeaveFrom) AND YEAR(LeaveTo) AND '1' BETWEEN MONTH(LeaveFrom) AND MONTH(LeaveTo)