1、查询特定时间的记录的SQL语句
--查询和今年相差八年的数据
SELECT title_id,title
FROM titles
where datediff(year,pubdate,getdate())=8
--ASP端的查询语句应写为
strSQL="SELECT title_id,title"& _
"FROM titles"& _
"WHERE datediff(year,pubdate,getdate())="&年变量
****************************************************************************************
--查询? 1991年10月21日的记录
select title_id,title
from titles
where datepart(yyyy,pubdate)=1991 and datepart(mm,pubdate)=10
and datepart(dd,pubdate)=21
--ASP端的查询语句应写成
strSQL="select title_id,title from titles "& _
"where datepart(yyyy,pubdate)="&年变量&" and datepart(mm,pubdate)="&月变量&" and "& _
"datepart(dd,pubdate)="&日变量
******************************************************************************************
2、查询某段时间内的表记录
--查询1991年6月1日至21日的表记录
select title_id,title,pubdate
from titles
where datepart(day,pubdate)$#@60;=datepart(day,getdate()) and datepart(day,pubdate)$#@62;=1 and
datepart(yyyy,pubdate)=1991
and datepart(month,pubdate)=6
--ASP端的查询语句应写为
strSQL="select title_id,title,pubdate from titles "& _
"where datepart(day,pubdate)$#@60;=datepart(day,getdate()) and datepart(day,pubdate)$#@62;=1 and "& _
"datepart(yyyy,pubdate)=1991 and datepart(month,pubdate)=6"
SQL Server中提供了功能强大的时间处理函数,这里给出的只是其中一些常用知识,希望大家能举一反三使用。 同时希望大家对不对或不足部分加以补充。