| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2100 人关注过本帖
标题:如果查询日期?
取消只看楼主 加入收藏
月夜NO风
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2008-6-29
结帖率:100%
收藏
 问题点数:0 回复次数:4 
如果查询日期?
SQL数据库中的字段是DATA,类型是DATETIME,长度是8,格式是YYYY-MM-DD.
但是SQL查询分析器上查询时会显示类似:“2007-01-04 00:00:00.000”
在一个ASP页面中,表单中输入比如“2007-01-03”,用于SQL查询时,
总是出错,提示:
错误类型:
Microsoft OLE DB Provider for SQL Server (0x80040E07)
从字符串转换为 datetime 时发生语法错误。
网上查询过好多方法,但是都没有效果。
我用的语句是这样的:
StrSQL=select * from table where dBillDay between '#"+Request.Form("DAY1")+"#' and '#"+Request.Form("DAY2")+"#'"
DAY1t DAY2分别是两个INPUT,但是输入日期查询就会出现:
从字符串转换为 datetime 时发生语法错误。
到底应该怎样做?
搜索更多相关主题的帖子: 查询 
2008-07-23 17:22
月夜NO风
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2008-6-29
收藏
得分:0 
[bo][un]dhdhzzw[/un] 在 2008-7-23 17:46 的发言:[/bo]

不是跟你说过了么?到现在还在问。。。。。。。。。。
杀了你。。。。。。别说你不能用,我一直这么写一直在用。,除非你哪里错了没说清楚。,,,。
StrSQL=select * from table where dBillDay between '"&forma ...

错误类型:
Microsoft VBScript 运行时错误 (0x800A000D)
类型不匹配: 'format'
我数据库的日期格式是“2006-12-27”这样子的啊。
DATETIME,长度8.。。。
StrSQL=StrSQL+"and dBillDay between '"&format(Request.Form("PurDAY1"),"YYYY-DD-MM ")&"' and '"&format(Request.Form("PurDAY2"),"YYYY-DD-MM")&"'"

全部代码是这样的:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#INCLUDE FILE="adovbs.inc"-->
<!--#INCLUDE FILE="dbtest_Conn.asp"-->
 
<html>
<% sId=Trim(Request.Form("txtsID"))
  ' 注意这里sId一般没有值,因为pur_list.asp是直接访问没有Post的数据。
%>
<% If session("sId")=""   then %>
  <% Response.Redirect("login.asp") %>
   <% else %>
   <% Response.Write("Welcome to Source Pro Industries Ltd.!")  %>
   <% end if  %>


<%
dim SqlConn,SqlRecSet
dim StrSQL
dim PurBn,PurBd,PurBs,PurBc,PurSr,PurRq,PurBrd,PurIn,PurBd2,PurBitPurDAY,PurDAY2
%>

<%
Function genSQLstr()
    Dim sId
    sId=session("sId")
    if Request.Form("PurBn")<>"" then
    StrSQL=StrSQL+"where sBillNo like '%"+Request.Form("PurBn")+"%'"
    else
    StrSQL=StrSQL+"where sSuppId='"& sId &"'"
    end if
    if Request.Form("PurBdY")<>"" or Request.Form("PurBdM")<>"" or Request.Form("PurBdD")<>""  then
    StrSQL=StrSQL+"and year(dBillDay)= '"& Request.Form("PurBdY")& "' and month(dBillDay)='"& Request.Form("PurBdM")& "' and day(dBillDay)='"& Request.Form("PurBdD")& "' "
    end if
    if Request.Form("PurBrdY")<>"" or Request.Form("PurBrdM")<>"" or Request.Form("PurBrdD")<>""  then
    StrSQL=StrSQL+"and year(dRecDate)= '"& Request.Form("PurBrdY")& "' and month(dRecDate)='"& Request.Form("PurBrdM")& "' and day(dRecDate)='"& Request.Form("PurBrdD")& "' "
    end if
    if Request.Form("PurDAY1")<>"" and Request.Form("PurDAY2")<>"" then
    StrSQL=StrSQL+"and dBillDay between '"&format(Request.Form("PurDAY1"),"YYYY-DD-MM ")&"' and '"&format(Request.Form("PurDAY2"),"YYYY-DD-MM")&"'"
    end if
    if Request.Form("PurBit")<>"" then
    StrSQL=StrSQL+"and sItemId like '%"+Request.Form("PurBit")+"%'"
    end if

    ' 只获取到自己的订单
    strSQL=strSQL & " and sSuppId='" & Replace(sId,"'","''") & "'" ' 防止注入攻击
End function
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>订单查询</title>
</head>
<body>

<%
OpenConn()
Set SqlRecSet=Server.CreateObject("ADODB.RECORDSET")
genSQLstr()
SqlRecSet.CursorType=adOpenStatic
StrSQL="select * from pur_order "+StrSQL
SqlRecSet.Open StrSQL,SqlConn,adLockReadOnly

%>


<div align="center">

<span class="pagenote1">订单查询</span>
</div>
<hr>
<form name="PurForm" method="post" >
<table width="663" align="center">
<tr>
<td width="655" bordercolor="#000000" bgcolor="#99CCCC">
<p>订单号码:
  <input name="PurBn" type="text" size=16>
  订单日期 :
  <input name="PurBdY" type="text" size=8>
  
  <input name="PurBdM" type="text" size=8>
  
  <input name="PurBdD" type="text" size=8>
  日</p>
<p>物料编码:
  <input name="PurBit" type="text" size=16>
交货日期 :
  <input name="PurBrdY" type="text" size=8>
  
  <input name="PurBrdM" type="text" size=8>
  
  <input name="PurBrdD" type="text" size=8>
  日</p>
  订单日期从:
    <input name="PurDAY1" type="text" size=16>
     到:<input name="PurDAY2" type="text" size=16>
<p>&nbsp;</p>
</p>
<center><input name="PurUp" value="查询" type="submit" ></center>
</table>
</form>
<div align="center">
 <center><b>共有<font color="#FF0000">(<%=SqlRecSet.RecordCount%>)</font>条记录符合查询要求</b></center>
 <table width="731" border="1" align="center"
 cellspacing="0" bordercolor="#000000"
bordercolorlight="#000000" bordercolordark="#FFFFFF">
  
</table>
<tr class="tableth1"><td height="22" width="725"><table width="853" border="1" align="center"
 cellspacing="0" bordercolor="#000000"
bordercolorlight="#000000" bordercolordark="#FFFFFF"><td width="847"><table width="885" border="1" align="center"
 cellspacing="0" bordercolor="#000000"
bordercolorlight="#000000" bordercolordark="#FFFFFF">
  <tr class="tableth1">
    <td height="22" width="125">订单号</td>
    <td height="22" width="82">订单日期</td>
    <td height="22" width="73">物料数量</td>
    <td height="22" width="114">物料编码</td>
    <td height="22" width="157">物料名称</td>
    <td height="22" width="70">供应商</td>
    <td height="22" width="96">交货日期</td>
    <td height="22" width="96">已收数量</td>
  </tr>
  <%
while not SqlRecSet.EOF
    PurBn=SqlRecSet.Fields("sBillNo").value
    PurBd=SqlRecSet.Fields("dBillDay").value
    Purbs=SqlRecSet.Fields("fQty").value
    PurBc=SqlRecSet.Fields("sItemId").value
    PurIn=SqlRecSet.Fields("sItemId_Name").value
    PurSr=SqlRecSet.Fields("sSuppId").value
    PurRq=SqlRecSet.Fields("fRecQty").value
    PurBrd=SqlRecSet.Fields("dRecDate").value
%>
  <td><%=PurBn%></td>
      <td><%=PurBd%></td>
    <td><%=PurBs%></td>
    <td><%=PurBc%></td>
    <td><%=PurIn%></td>
    <td><%=PurSr%></td>
    <td><%=PurBrd%></td>
    <td><%=PurRq%></td>
  </tr>
  <%
    SqlRecSet.MoveNext
wend
SqlRecSet.Close
SqlConn.Close
%>
</table></td>
    </tr>
</table></td>
</tr>
</body>
</html>
2008-07-23 20:25
月夜NO风
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2008-6-29
收藏
得分:0 
[bo][un]multiple1902[/un] 在 2008-7-23 18:19 的发言:[/bo]

“如果查询日期?”

???

不好意思,打错了。。。
是如何。。。。。。
2008-07-23 20:25
月夜NO风
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2008-6-29
收藏
得分:0 
出差好几天,回来后,测试,终于可以:
正确的是:
StrSQL=StrSQL+"and dBillDay between '"& formatdatetime(Request.Form("PurDAY1"),2)&"' and '"& formatdatetime(Request.Form("PurDAY2"),2)&"'"

引用:

函数调用格式:FormatDateTime(Date[, NamedFormat])

FormatDateTime函数的语法有以下参数:
参数 描述
Date 必选,要被格式化的日期表达式。
NamedFormat 可选,指示所使用的日期/时间格式的数值,如果省略,则使用vbGeneralDate。

设置
NamedFormat参数可以有以下值:
vbGeneralDate 0 显示日期和/或时间;如果有日期部分,则将该部分显示为短日期格式;如果有时间部分,则将该部分显示为长时间格式;如果都存在,则显示所有部分。
vbLongDate 1 使用计算机区域设置中指定的长日期格式显示日期。
vbShortDate 2 使用计算机区域设置中指定的短日期格式显示日期。
vbLongTime 3 使用计算机区域设置中指定的时间格式显示时间。
vbShortTime 4 使用24小时格式(hh:mm)显示时间。

示例:FormatDateTime(Now,2),得到的将是短日期格式,如2007-5-3。
2008-07-28 17:32
月夜NO风
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2008-6-29
收藏
得分:0 
[bo][un]dhdhzzw[/un] 在 2008-7-29 11:15 的发言:[/bo]

不就是这样跟你说的么////。。。。。。。

你说用YYYY-M-D的格式,我这样不行,但是用2,就是代表VBLONGDAY那个就可以。
2008-07-30 16:59
快速回复:如果查询日期?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015985 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved