| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2100 人关注过本帖
标题:如果查询日期?
只看楼主 加入收藏
月夜NO风
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2008-6-29
结帖率:100%
收藏
 问题点数:0 回复次数:14 
如果查询日期?
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
dhdhzzw
Rank: 1
等 级:新手上路
帖 子:949
专家分:0
注 册:2007-8-13
收藏
得分:0 
不是跟你说过了么?到现在还在问。。。。。。。。。。
。。。。。。别说你不能用,我一直这么写一直在用。,除非你哪里错了没说清楚。,,,。
StrSQL=select * from table where dBillDay between '"&formatdatetime(Request.Form("DAY1"),"yyyy-M-d")&"' and '"&formatdatetime(Request.Form("DAY2"),,"yyyy-M-d")&"'"

注意,日期格式"YYYY-M-D"换成你数据库所用的日期格式就可以了。。。。。

[[it] 本帖最后由 dhdhzzw 于 2008-7-24 09:14 编辑 [/it]]
2008-07-23 17:46
multiple1902
Rank: 8Rank: 8
等 级:贵宾
威 望:42
帖 子:4881
专家分:671
注 册:2007-2-9
收藏
得分:0 
“如果查询日期?”

???
2008-07-23 18:19
月夜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
multiple1902
Rank: 8Rank: 8
等 级:贵宾
威 望:42
帖 子:4881
专家分:671
注 册:2007-2-9
收藏
得分:0 
Format是什么?格式化?
2008-07-23 22:07
multiple1902
Rank: 8Rank: 8
等 级:贵宾
威 望:42
帖 子:4881
专家分:671
注 册:2007-2-9
收藏
得分:0 
那个,谁教你ASP里有Format函数的,帮我鄙视一下他。就这么多。
2008-07-23 22:08
dhdhzzw
Rank: 1
等 级:新手上路
帖 子:949
专家分:0
注 册:2007-8-13
收藏
得分:0 
[bo][un]multiple1902[/un] 在 2008-7-23 22:08 的发言:[/bo]

那个,谁教你ASP里有Format函数的,帮我鄙视一下他。就这么多。

  formatdatetime 我只想告诉该怎样格式化,我省略写的。
作为版主说出鄙视别人这种话,你觉得合适么?你说个对的帮他解决不就行了么?
2008-07-24 09:13
multiple1902
Rank: 8Rank: 8
等 级:贵宾
威 望:42
帖 子:4881
专家分:671
注 册:2007-2-9
收藏
得分:0 
[bo][un]dhdhzzw[/un] 在 2008-7-24 09:13 的发言:[/bo]


  formatdatetime 我只想告诉该怎样格式化,我省略写的。
作为版主说出鄙视别人这种话,你觉得合适么?你说个对的帮他解决不就行了么?

我看N多人上了这个format函数的当了。

asp里本没有format函数,但是写的人多了,真的有人会误以为有的,刚好VB里又有Format函数的介绍,这样对菜鸟的误导是很深的。

更何况这回是他抄错了,而且错误提示也很清楚,他居然没有检查代码而查出这个错误,直接把代码发上来,窃以为不妥。

[[it] 本帖最后由 multiple1902 于 2008-7-24 11:26 编辑 [/it]]
2008-07-24 11:25
tianyu123
Rank: 1
等 级:新手上路
威 望:2
帖 子:576
专家分:0
注 册:2007-8-26
收藏
得分:0 
去掉 # 试试
StrSQL=select * from table where dBillDay between '#"+Request.Form("DAY1")+"#' and '#"+Request.Form("DAY2")+"#'"

改为:

StrSQL=select * from table where dBillDay between '"+Request.Form("DAY1")+"' and '"+Request.Form("DAY2")+"'"

[[it] 本帖最后由 tianyu123 于 2008-7-24 11:58 编辑 [/it]]

改变一切,须从改变观念开始!
2008-07-24 11:57
快速回复:如果查询日期?
数据加载中...
 
   



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

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