ASP和SQLServer时间处理方法

        在开发数据库应用中,经常会遇到处理时间的问题,如查询指定时间的记录等。下面就这些常见的问题,结合自己的一些经验,和大家探讨一下这类问题。
  首先介绍一下,SQL Server里处理时间的几个主要函数的用法:


getdate()函数:取得系统当前的日期和时间。返回值为datetime类型的。
用法:getdate()
例子:
select getdate() as dte,dateadd(day,-1,getdate()) as nowdat
输出结果:
dte nowdat
--------------------------- ---------------------------
1999-11-21 19:13:10.083 1999-11-20 19:13:10.083

(1 row(s) affected)

datepart()函数:以整数的形式返回时间的指定部分。
用法:datepart(datepart,date)
参数说明:datepart时要返回的时间的部分,常用取值year、month、day、hour、minute。
date是所指定的时间。
例子:
SELECT DATEPART(month, GETDATE()) AS 'Month Number'
输出结果:
Month Number
------------
11

(1 row(s) affected)

dateadd()函数:通过给指定的时间的指定部分加上一个整数值以返回一个新时间值。
用法:dateadd(datepart,number,date)
参数说明:datepart(同上)
date(同上)
number要增加的值,整型,可正可负,正值返回date之后的时间值,负值返回date
之前的时间值
例子:
select getdate() as today
select dateadd(day,-1,getdate())
select dateadd(day,1,getdate())
输出:
today
---------------------------
1999-11-21 19:42:41.410

(1 row(s) affected)

yesterday
---------------------------
1999-11-20 19:42:41.410

(1 row(s) affected)

tomorrow
---------------------------
1999-11-22 19:42:41.410

(1 row(s) affected)

datediff()函数:返回两个时间以指定时间部分来计算的差值。返回整数值。

如1991-6-12和1991-6-21之间以天来算相差9天,1998-6-12和1999-6-23按年算相差1年,1999-12-1和1999-3-12按月算相差9个月

用法:datediff(darepart,date1,date2)
参数说明:datepart(同上)
date1、date2(同上date)
例子:
select datediff(month,'1991-6-12','1992-6-21') as a
输出:
a
-----------
12

(1 row(s) affected)


结合上一节介绍的几个函数,这一节介绍ASP里相关的处理方法,利用了SQL Server里的示例数据库pubs中的titles表
的title_id、title、pubdate字段。表中数据如下:
title_id title pubdate


--------------------------------------------------------------------------------
BU1032 The Busy Executive's Database Guide 1991-06-12 00:00:00.000
BU1111 Cooking with Computers: Surreptitious Balance Sheets 1991-06-09 00:00:00.000
BU2075 You Can Combat Computer Stress! 1991-06-30 00:00:00.000
BU7832 Straight Talk About Computers 1991-06-22 00:00:00.000
MC2222 Silicon Valley Gastronomic Treats 1991-06-09 00:00:00.000
MC3021 The Gourmet Microwave 1991-06-18 00:00:00.000
MC3026 The Psychology of Computer Cooking 1998-11-13 03:10:53.657
PC1035 But Is It User Friendly? 1991-06-30 00:00:00.000
PC8888 Secrets of Silicon Valley 1994-06-12 00:00:00.000
PC9999 Net Etiquette 1998-11-13 03:10:53.670
PS1372 Computer Phobic AND Non-Phobic Individuals: Behavior Variations 1991-10-21 00:00:00.000
PS2091 Is Anger the Enemy? 1991-06-15 00:00:00.000
PS2106 Life Without Fear 1991-10-05 00:00:00.000
PS3333 Prolonged Data Deprivation: Four Case Studies 1991-06-12 00:00:00.000
PS7777 Emotional Security: A New Algorithm 1991-06-12 00:00:00.000
TC3218 Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean 1991-10-21 00:00:00.000
TC4203 Fifty Years in Buckingham Palace Kitchens 1991-06-12 00:00:00.000
TC7777 Sushi, Anyone? 1991-06-12 00:00:00.000

(18 row(s) affected)

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)<=datepart(day,getdate()) and datepart(day,pubdate)>=1 and
datepart(yyyy,pubdate)=1991
and datepart(month,pubdate)=6

--ASP端的查询语句应写为

strSQL="select title_id,title,pubdate from titles "& _
"where datepart(day,pubdate)<=datepart(day,getdate()) and datepart(day,pubdate)>=1 and "& _
"datepart(yyyy,pubdate)=1991 and datepart(month,pubdate)=6"


  SQL Server中提供了功能强大的时间处理函数,这里给出的只是其中一些常用知识,希望大家能举一反三使用。 同时希望大家对不对或不足部分加以补充
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值