MSSQL时间日期问题

bulls5988 2010-09-16 10:50:54
我现在用的时间控件取得的时间为:2010-1-1 赋值给变量 sj

我的MSSQL中的字段 INPUT_TIME 数据格式为: 2010-1-1 下午 15:00:00

如果我想把我取得的 2010-1-1 作为查询条件,取得时间 2010-1-1 这天的数据,
select * from xx where INPUT_TIME ='"+sj+"'

应该如何处理?



...全文
160 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
bulls5988 2010-09-19
  • 打赏
  • 举报
回复
原因已经查明 if(hasrow) 就OK了 原来是 if(hasrow&&read())
打一壶酱油 2010-09-19
  • 打赏
  • 举报
回复
sj 不要直接 ''包围, 加上 cast('"+ sj+"' as datetime) 试一试
bulls5988 2010-09-18
  • 打赏
  • 举报
回复
调试没发现什么问题,应该是那天的找到了。但是没法输出 是我用DATAREADER的原因吗?
huangwenquan123 2010-09-18
  • 打赏
  • 举报
回复
断点试一下就什么都知道了
huangwenquan123 2010-09-18
  • 打赏
  • 举报
回复
= =悲剧!我的错!没看好!
bulls5988 2010-09-18
  • 打赏
  • 举报
回复
和今天日期有什么关系 我数据库里的日期是 9月2号的啊
huangwenquan123 2010-09-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 bulls5988 的回复:]
的确查找应该是没有问题,但是却不能输出。


C# code

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using Syst……
[/Quote]
今天都9月18号了,你查找9月2号的当然找不出!
你想查找几天范围内的把0改成你想要的
苏州牛恋歌 2010-09-18
  • 打赏
  • 举报
回复
select * from xx where substring(INPUT_TIME,1,8) ='"+sj+"'
bulls5988 2010-09-18
  • 打赏
  • 举报
回复
的确查找应该是没有问题,但是却不能输出。


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conn"]);
string ks = ksrq.Value;
string sql = "select * from zj_leiji_mingxi where datediff(d,'"+ks+"',input_time)=0";
SqlCommand command = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader rs = command.ExecuteReader();
if (rs.HasRows && rs.Read())
{
while (rs.Read())
{
Response.Write("赠奖组别:" + rs["input_time"] );
}
}
else
{
Response.Write("没有相关赠奖记录!");
}

}
}


现在我数据库里有2010-09-02 08:23:59 的数据 选中这一天,查找就没有输出。

但是如果选择别的日期就输出 "没有相关赠奖记录!"

有人知道其中原因吗?
softswitcher 2010-09-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyq11 的回复:]
string sql="select * from xx where datediff(d,INPUT_TIME,'"+sj+"')=0"
[/Quote]
对,书上就是这么写的。
神码浮云 2010-09-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyq11 的回复:]
string sql="select * from xx where datediff(d,INPUT_TIME,'"+sj+"')=0"
[/Quote]

正解...
hua_lanyu 2010-09-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyq11 的回复:]
string sql="select * from xx where datediff(d,INPUT_TIME,'"+sj+"')=0"
[/Quote]

可以实现
wuyq11 2010-09-16
  • 打赏
  • 举报
回复
string sql="select * from xx where datediff(d,INPUT_TIME,'"+sj+"')=0"

bulls5988 2010-09-16
  • 打赏
  • 举报
回复
无语 暴走中...
softswitcher 2010-09-16
  • 打赏
  • 举报
回复
反正不是这本,就是另外一本。
softswitcher 2010-09-16
  • 打赏
  • 举报
回复
记不得了,你找找。
bulls5988 2010-09-16
  • 打赏
  • 举报
回复
杯具 是哪本书的什么地方啊...
softswitcher 2010-09-16
  • 打赏
  • 举报
回复
记得一本书上有的,很简单。

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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