正在加载中...

关闭
请选择需要拨打的号码

信息详页

返回
ASP中SQL和Access数据库中时间/日期字段内容查询的方法说明

方法如下:
MS SQL数据库:
SQL="select * from 表名 where 时间/日期字段 between '"& 传递过来开始的时间值 &"' and '"& DateAdd("d",1,""&request.Form("endtime")&"")&"'"

Access数据库:
SQL="select * from 表名 where Ap_addtime between #"& 传递过来开始的时间值 &"# and #"&DateAdd("d",1,""&request.Form("endtime")&"")&"# ..."


今天做产品高级搜索遇到了时间段搜索,发现了一些常犯的错误

1. 把日期当作字符串来操作,在SQL语句中,其实日期和数字一样,都不能加'字符串',否则会出现语法错误

2. 选择某个日期中间的记录可以用between and 而不是用< > =等符号,而且日期之间也有讲究,access数据库的日期间用#日期#隔开,mssql用'单引号',如:

timestr类型为日期/时间

Dim dateStr1,DateStr2
DateStr1="2006-8-10"
DateStr2=Date()
sql="select * from 表名 where 时间/日期字段 between #"& 开始的时间 &"# and #"& 结束的时间 &"#"


ASP比较时间的SQL语句,其中d为天数!

access
select * from [table] where datediff('d',时间字段,Now())=0


Sql Server
select * from [table] where datediff(d,时间字段,GetDate())=0