WEB开发网
开发学院数据库MSSQL Server sql server 函数介绍-日期类 阅读

sql server 函数介绍-日期类

 2008-12-10 10:14:45 来源:WEB开发网   
核心提示: 8、查询指定日期段内过生日的人员--测试数据DECLARE @t TABLE(ID int,Name varchar(10),Birthday datetime)INSERT @t SELECT 1,'aa','1999-01-01'UNION ALL SE

8、查询指定日期段内过生日的人员

--测试数据
DECLARE @t TABLE(ID int,Name varchar(10),Birthday datetime)
INSERT @t SELECT 1,'aa','1999-01-01'
UNION ALL SELECT 2,'bb','1996-02-29'
UNION ALL SELECT 3,'bb','1934-03-01'
UNION ALL SELECT 4,'bb','1966-04-01'
UNION ALL SELECT 5,'bb','1997-05-01'
UNION ALL SELECT 6,'bb','1922-11-21'
UNION ALL SELECT 7,'bb','1989-12-11'
  
DECLARE @dt1 datetime,@dt2 datetime
  
--查询 2003-12-05 至 2004-02-28 生日的记录
SELECT @dt1='2003-12-05',@dt2='2004-02-28'
SELECT * FROM @t
WHERE DATEADD(Year,DATEDIFF(Year,Birthday,@dt1),Birthday)
    BETWEEN @dt1 AND @dt2
  OR DATEADD(Year,DATEDIFF(Year,Birthday,@dt2),Birthday)
    BETWEEN @dt1 AND @dt2
/**//*--结果
ID     Name    Birthday
---------------- ---------------- --------------------------
1      aa     1999-01-01 00:00:00.000
7      bb     1989-12-11 00:00:00.000
--*/
  
--查询 2003-12-05 至 2006-02-28 生日的记录
SET @dt2='2006-02-28'
SELECT * FROM @t
WHERE DATEADD(Year,DATEDIFF(Year,Birthday,@dt1),Birthday)
    BETWEEN @dt1 AND @dt2
  OR DATEADD(Year,DATEDIFF(Year,Birthday,@dt2),Birthday)
    BETWEEN @dt1 AND @dt2
/**//*--查询结果
ID     Name    Birthday
---------------- ----------------- --------------------------
1      aa     1999-01-01 00:00:00.000
2      bb     1996-02-29 00:00:00.000
7      bb     1989-12-11 00:00:00.000
--*/

上一页  2 3 4 5 6 7 8 9 10  下一页

Tags:sql server 函数

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接