读取数据库内储存的时间段内的数据

读取数据库内存储的时间段内的数据
数据库表中有两个字段,startday,endday;

我现在要判断此刻的时间是不是在startday和endday之间,
若在此之间,则读取数据,否则不读取。

读取数据库表的函数有GetTable(),参数为sql语句;
------解决方案--------------------
select * from table where @data>=startday and  @data<= endday

------解决方案--------------------
本帖最后由 bdmh 于 2014-05-13 10:12:19 编辑
select * from table where yourdatefield between begindate and endate

------解决方案--------------------
引用:
select * from table where yourdatefield between begindate and endate



顶。。。

------解决方案--------------------
引用:
Quote: 引用:

select * from table where @data>=startday and  @data<= endday


这个@data是什么? 我数据库里没有保存现在的日期呀



@data就是你说的此刻的时间,一个变量,随意命名的
------解决方案--------------------
 查 datefdiff  的用法。 
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

select * from table where @data>=startday and  @data<= endday


这个@data是什么? 我数据库里没有保存现在的日期呀



@data就是你说的此刻的时间,一个变量,随意命名的



   dt = ititemBll.GetDataTable("RID='" + RRid + "'" + "And Del = 'F'" + "And @now>STARTDAY" + "@now<ENDDAY");
 
public DataTable GetDataTable(string filter)
        {
            string sql = string.Format("SELECT * FROM {0} WHERE {1} ", TName, filter);
            return SqlHelper.ExecuteDataTable(ProDao.GetConnStr(), CommandType.Text, sql, null);
        }


帅哥,你的@now没有赋值,你直接这种形式吧
DateTime now= DateTime.Now;
  dt = ititemBll.GetDataTable("RID='" + RRid + "'" + "And Del = 'F' And "+now+">STARTDAY" + now+"<ENDDAY");

------解决方案--------------------
select  * from table where getdate()>=startday and getdate()<=endday
------解决方案--------------------
引用:
Quote: 引用:

select  * from table where getdate()>=startday and getdate()<=endday

这个getdate要怎么弄?

这是sqlserver自带的获取当天日期的函数,你想怎么弄
------解决方案--------------------
引用:
这个startday和endday是string,该怎么办?

首先你明知道要做日期比较为什么要把字段设计成string,然后就是如果没法改数据库,就去找sql强转日期的函数