数据库中日期大小的判断

数据库中表字段 pubdate ,如设置初始默认值为 getdate() ,则最后产生的日期为:2006-10-17 或 2006-01-06 这样的格式数据。

 

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

 

假如做查询,从前台取过来的日期 enddate 为 "2006-10-17" ,注意此日期无时,分秒。

如果想要做一个查询 ,把表中数据所有小于等于 2006-10-17 日期的数据筛选出来,如果查询语句这样写的话:

 

select * from 表 where pubdate<='"+enddate+"' ,则只能取出 2006-10-17 日以前的数据,2006-10-17当日的数据出不来,因为 2006-10-17 相当于 2006-10-17 00:00:00

 

解决方法:

DateTime dtn = DateTime.Parse(this.TextBox2.Text.Trim().ToString());

1 where pubdate<= '"+dtn.AddDays(1).ToShortDateString()+"'

2 where convert(varchar(10),DateAndTime,120) = '" +this.TextBox2.Text.Trim().ToString()+ "'

 

        DateTime dt  =  DateTime.Now;
        
string  dts  =  dt.ToShortDateString();  // 形式:2011-9-21 ,注意,是9 而不是 09 ,不能用这个字符串与数据库日期进行比较。
        DateTime dtf  =  DateTime.Parse(dts);  //形式:2011-9-21 0:00:00 会自动把时间加上,但是全是 0
        
string  dtsm  =  dt.ToShortTimeString(); // 形式:8:41 ,8:01   ,是8 不是 08:41,但是后面的分钟是两位数的。

         int  hm  =  int .Parse(dt.Hour.ToString())  *  60  +  int .Parse(dt.Minute.ToString());
        
string   vnum  =  " V " + ((hm  -  (hm  %  15 ))  /  15 ).ToString();
      
        
int  nowyear  =  dt.Year;  //  2011
         int  nowmonth  =  dt.Month;  // 9 不是 09
         int  nowday  =  dt.Day;  // 3 不是 03

 

 

       // 数据库中  datepart(month, DateAndTime)  也为 9 不是 09
        
// 数据库中  datepart(day, DateAndTime)  也为 3 不是 03
        
// select convert(varchar(10),提交日期,120) from adslmoney  得到的提交日期为:2010-05-04  月份和日子,都是双位数 05-04

         string  DateFormats  =  Convert.ToDateTime(dt).ToString( " yyyy-MM-dd " );  // 但是这样的话,月份和日子,就是双位数2011-09-21 

 
// string sqlcheck = "select ID from HeatLine where PID = '" + pipeid + "' and  datepart(year, DateAndTime) = " + nowyear + " and  datepart(month, DateAndTime) = " + nowmonth + " and   datepart(day, DateAndTime) = " + nowday + "";
         string  sqlcheck  =  " select ID from HeatLine where PID = ' "  +  pipeid  +  " ' and convert(varchar(10),DateAndTime,120) = ' "  +  DateFormats  +  " ' " ;

 

转载于:https://www.cnblogs.com/tiger8000/archive/2011/09/13/2174836.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值