出错!如何选择一个日期从数据库读出属于是这个日期范围内的新闻!!!!!!!再问

zytp 2003-11-08 10:58:45
http://expert.csdn.net/Expert/topic/2437/2437640.xml?temp=.5842707
上面那贴子不知跑哪页了,再开贴提问!
数据库是
Sql Server
ndate是smalldatetime类型,读不出来我选择的日期范围!
<!--#include file="../manage/conn.asp"-->
<%
'date1=datevalue(request("nian1") & "-" & request("yue1") & "-" & request("ri1"))
'date2=datevalue(request("nian2") & "-" & request("yue2") & "-" & request("ri2"))
date1=DateSerial(request("nian"),request("yue"),request("ri"))
date2=DateSerial(request("nian2"),request("yue2"),request("ri2"))

response.write date1 & "为起始日期<BR>"
response.write date2 & "为终止日期<BR><BR>"
Set rs=Server.createobject("adodb.recordset")
sql="select * from news where ndate>=" & date1' & " and ndate<=" & date2
'sql="select * from news where ndate between 'date1' and 'date2' "
Response.write sql
'response.end
rs.open sql,conn,1,1
if rs.eof then
Response.write "暂无"
%>
<%
else
do while not rs.eof
%>
<%=rs("topic")%>      <%=rs("ndate")%><BR>
<%
rs.movenext
loop
end if
rs.close

set rs=nothing
conn.close
set conn=nothing%>
...全文
60 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
超级大笨狼 2003-12-22
  • 打赏
  • 举报
回复
possible_Y
FormatDateTime函数写错了
Visual Basic Scripting Edition

FormatDateTime 函数
请参阅
FormatCurrency 函数 | FormatNumber 函数 | FormatPercent 函数
要求
版本2
返回表达式,此表达式已被格式化为日期或时间。

FormatDateTime(Date[, NamedFormat])
参数
Date
必选项。要被格式化的日期表达式。
NamedFormat
可选项。指示所使用的日期/时间格式的数值,如果省略,则使用 vbGeneralDate。
设置
NamedFormat 参数可以有以下值:

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

说明
下面例子利用 FormatDateTime 函数把表达式格式化为长日期型并且把它赋给 MyDateTime:

Function GetCurrentDate
'FormatDateTime 把日期型格式化为长日期型。
GetCurrentDate = FormatDateTime(Date, 1)
End Function
要求
版本2

请参阅
FormatCurrency 函数 | FormatNumber 函数 | FormatPercent 函数



--------------------------------------------------------------------------------

© 2001 Microsoft Corporation. 保留所有权利。

angelheavens 2003-11-08
  • 打赏
  • 举报
回复
up
zytp 2003-11-08
  • 打赏
  • 举报
回复
sql="select * from news where datediff(day, '"&date1&"', ndate)>-1 and datediff(day, '"&date2&"', ndate)<1 order by ndate"

找到高手了这么解决的,嘿,你们都来捧场都给分!
此高手我同事,厉害,!
Tal 2003-11-08
  • 打赏
  • 举报
回复
SQL Server的函数我不熟悉
你可用cdate()这类函数转换后再比较
2003-01-01
2003-1-1
jbj001 2003-11-08
  • 打赏
  • 举报
回复
ndate>=" & date1' & " and ndate<=" & date2
ndate是否是时间类型,是的话ndate>=#" & date1 & "# and ndate<=#" & date2 & "#
possible_Y 2003-11-08
  • 打赏
  • 举报
回复
sql="select * from news where ndate between '"& FormatDateTime(2,date1) &"' and '"& FormatDateTime(2,date2) &"'"
mjb001 2003-11-08
  • 打赏
  • 举报
回复
sql="select * from news where ndate>=" & date1' & " and ndate<=" & date2

看见没有,少了个“。
sql="select * from news where ndate>=" & date1' & " and ndate<=" & date2"

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧