2012-08-01 158 views
0

我已日期时间存储在我的表中的字段LeaveFrom和LeaveTo的叶子,像日期时间基于查询2008

LeaveFrom = 05/26/2012 12:00:00和LeaveTo = 06 /二千零十二分之三十零..等等

从前端我传递只月份和年份。那么相同的搜索条件是什么。

我需要年月明智的搜索条件。有多少人申请休假/或在特定月份缺席。

感谢所有的你..几乎所有已接近预期...谢谢,我需要的是一个“提示” ..感谢一吨到所有你们的..

+1

嗯,这取决于你在找什么过滤器。你需要详细说明一些。 – 2012-08-01 10:25:18

+1

你想要什么结果?建议您最好给出示例数据和示例结果。 – MatBailie 2012-08-01 10:27:28

+0

如果我想检查第5个月有多少人休假,该怎么办? – 2012-08-01 10:27:52

回答

1

据我所知,您希望所有在所选月份中具有在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 
+0

这仅仅是我还是不是非常低效,因为您将为每条记录调用dateadd()两次? – 2012-08-02 10:40:36

+0

@GermannArlington你是对的,但它是唯一正确的解决方法,至少据我所知。我最终用数据中的例子来测试你的解决方案。 – 2012-08-03 09:18:46

+1

我只是试过你的数据集和选择,它给了我相同的结果。我真的不明白为什么它不应该? 这里是我使用的代码... '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; ' – 2012-08-03 09:45:57

2

假设你想获得一组的人谁是休假在一个给定的月/年:

以下是完整的更新代码,有些人可能会觉得有用:

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语句。我这样写,使其易于阅读。

1
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} 
+0

如果您使用<而不是<= – 2012-08-01 10:31:47

+0

@NikolaMarkovinović您不需要减去任何东西好点。谢谢! – 2012-08-01 10:33:13

+0

但是你在这个假设下正在执行日期算术。 – 2012-08-01 10:37:41

2

尝试:

where (MONTH(LeaveFrom) = @Month 
     and YEAR(LeaveFrom) = @Year) 
     OR 
     (and MONTH(LeaveTo) = @Month 
     and YEAR(LeaveTo) = @Year) 
+0

我认为你需要一个来自和去日期之间的OR – 2012-08-01 10:33:06

+0

@GermannArlington:这是真的..谢谢你.. – 2012-08-01 10:35:29

+0

如果有任何列索引是不理想的。查看我发布的方法 – Madhivanan 2012-08-01 11:32:36

1

.... 其中DATEPART(YYYY,LeaveFrom)= 2012和DATEPART(米,LeaveFrom)= 5 ....

1
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) 
1

试试这个。Performancewise这会做最好的

declare @month int, @year int 
select @month=6, @year=2012 

select columns from table 
where 
LeaveFrom>=Dateadd(month,(@year-1900)*[email protected],0) and 
LeaveTo<Dateadd(month,(@year-1900)*[email protected],0) 

您可能还需要知道如何模拟DateSerial函数以不同的方式 http://beyondrelational.com/modules/2/blogs/70/posts/15788/10-ways-to-simulate-dateserial-function.aspx

+0

? – 2012-08-01 12:08:38

0

DECLARE @temp TABLE( ID INT IDENTITY(1,1) ,LeaveFrom DATETIME ,LeaveTo DATETIME ,用户名VARCHAR(100) )

INSERT INTO @temp VALUES( '2015年1月1日 , '2015年1月5日' '拉梅什' ) ,( '2015年1月7日' , '2015年1月9日' '苏雷什' ) ,( “2015 - 01-03' '2015年1月6日' '的Dinesh' ) ,( '2015年1月15日' '2015年1月25日' '卡姆利什' ) ,( '2015-01-12' ,'2015-01-17' ,'Mohan' )

SELECT COUNT(*)FROM @temp 其中 '2015年' 之间YEAR(LeaveFrom) 和Year(LeaveTo) 和 '1' 之间MONTH(LeaveFrom) 和月(LeaveTo)