Sql Server 2008中基于日期时间的查询

时间:2012-08-01 10:22:53

标签: sql sql-server sql-server-2008 tsql

我的日期时间存储在我的表中的LeaveFrom和LeaveTo字段中,如

LeaveFrom = 05/26/2012 12:00:00和LeaveTo = 06/30/2012 ..等等

从前端开始,我只通过了月份和年份。那么同样的搜索条件是什么。

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

感谢所有人......几乎所有人都接近预期......谢谢,我需要的是一个'提示'..感谢你们所有人......

8 个答案:

答案 0 :(得分: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

我只计算一次日期,而不是为每个选定的行计算日期。上面的代码也可以很容易地转换成单个SQL语句。我这样写是为了让它易于阅读。

答案 1 :(得分:1)

试试这个:

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

答案 2 :(得分:0)

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}

答案 3 :(得分:0)

....     其中DATEPART(yyyy,LeaveFrom)= 2012和DATEPART(m,LeaveFrom)= 5 ....

答案 4 :(得分:0)

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)

答案 5 :(得分:0)

据我所知,你想要所有在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

答案 6 :(得分:0)

试试这个。 Performancewise这将是最好的

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)

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

答案 7 :(得分:0)

DECLARE @Temp TABLE(     ID INT IDENTITY(1,1)     ,LeaveFrom DATETIME     ,LeaveTo DATETIME     ,UserName VARCHAR(100)     )

INSERT INTO @Temp 价值观(     “2015年1月1日”     “2015年1月5日”     “拉梅什”     )     (     “2015年1月7日”     “2015年1月9日”     “苏雷什”     )     (     “2015年1月3日”     “2015年1月6日”     “迪内希”     )     (     “2015年1月15日”     “2015年1月25日”     “卡姆利什”     )     (     '2015-01-12'     “2015年1月17日”     ,“莫汉”     )

SELECT COUNT(*) 来自@Temp 年度'2015'年份(LeaveFrom)         和年(LeaveTo)     和'1'在一个月之间(LeaveFrom)         和月(LeaveTo)

相关问题