日期范围查询无法正常工作

时间:2013-12-17 07:21:31

标签: date between

我正在使用以下代码来过滤某些日期范围的结果。它只是给我带来了一些错误的结果......就像我使用的过滤器是错误的。

这是代码:

<html>
<head>
<title>Classic ASP Search Access Database Demo</title>
<style>
 body {font-family:arial;}
 table.search {width:200px;}
table.recordset {width:600px;padding:0px;}
div.recordset {width:618px;height:300px;overflow-y:scroll;border:1px solid black;}
th.recordset {color:white;background:black;padding:2px;}
.search {width:100px;}
.column-1 {width:200px;text-align:left;}
.column-2 {width:200px;text-align:left;}
.column-3 {width:200px;text-align:left;}
.eof {color:red;padding:4px;}
td.recordset {border-bottom: 1px solid silver;}
</style>
</head>
<body>

<%


sSearch1 = Request.Form("search1")
sSearch2 = Request.Form("search2")


%>

<form action="1emvolio.asp" method="post">
<table class="search">
<tr>from
 <td class="search"><input name="search1" type="date" value="<%=sSearch1%>"></td>

</tr>

    <tr>to
 <td class="search"><input name="search2" type="date" value="<%=sSearch2%>"></td>
 <td class="search"><input type="submit" value="Search">
 </tr>
 </table>
 </form>

<h3>Search Results</h3>

<div class="recordset"> 
<table class="recordset" cellspacing="0">
<tr>
 <th class="column-1 recordset">Description 1</th>
 <th class="column-2 recordset">Description 2</th>
 <th class="column-3 recordset">Description 3</th>
</tr>

<%


If sSearch1  <> "" Then




sSearch1 = Trim(Replace(sSearch1,"'","''"))



Set oConnection = Server.CreateObject("ADODB.Connection")

oConnection.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")


Set oRecordset = oConnection.Execute("SELECT * " & _
                                    "FROM emvolio " & _
                                    "WHERE next_date between '" & sSearch1 & "' and '" & sSearch2  "'")



If not oRecordset.EOF Then
 Do While not oRecordset.EOF
%>

<tr>
 <td class="column-1 recordset"><%=oRecordset("chip")%></td>   
 <td class="column-2 recordset"><%=oRecordset("emvolio")%></td>
 <td class="column-3 recordset"><%=oRecordset("next_date")%></td>
</tr>

<%
oRecordset.MoveNext
Loop
Else
%>

<tr>
<td class="eof" colspan="3">No Records</td>
</tr>

<%

end if
end if
response.write(sSearch2)
 response.write(sSearch1)

%>

</table>
</div>
</body>
</html>

我认为问题是字段“next_date”不是DATE字段,因此它只读取每个第一个dd / mm / yyyy。 在我正在使用的访问数据库中,我必须使用“next_date”字段的文本类型才能使.asp页面正常工作。当我使用日期类型时,我收到错误。

请帮助

1 个答案:

答案 0 :(得分:0)

也许CONVERT命令可能有效

http://www.codeproject.com/Articles/576178/cast-convert-format-try-parse-date-and-time-sql

&#34; SELECT * FROM emvolio WHERE转换(datetime,next_date,101)在&#39;&#34;之间&安培; sSearch1&amp; &#34;&#39;和&#39;&#34; &安培; sSearch2&#34;&#39;&#34;