SQL语句写返回一天内的纪录,得到一周内星期几的时间

#region 获取星期几对应的时间
week
= DateTime.Today.DayOfWeek.ToString();
switch (week)
{
case "Monday":
week
= "星期一";
MondayTime
= DateTime.Today;
TuesdayTime
= DateTime.Today.AddDays(1);
WednesdayTime
= DateTime.Today.AddDays(2);
ThursdayTime
= DateTime.Today.AddDays(3);
FridayTime
= DateTime.Today.AddDays(4);
SaturdayTime
= DateTime.Today.AddDays(5);
SundayTime
= DateTime.Today.AddDays(6);
break;
case "Tuesday":
week
= "星期二";
MondayTime
= DateTime.Today.AddDays(-1);
TuesdayTime
= DateTime.Today;
WednesdayTime
= DateTime.Today.AddDays(1);
ThursdayTime
= DateTime.Today.AddDays(2);
FridayTime
= DateTime.Today.AddDays(3);
SaturdayTime
= DateTime.Today.AddDays(4);
SundayTime
= DateTime.Today.AddDays(5);
break;
case "Wednesday":
week
= "星期三";
MondayTime
= DateTime.Today.AddDays(-2);
TuesdayTime
= DateTime.Today.AddDays(-1);
WednesdayTime
= DateTime.Today;
ThursdayTime
= DateTime.Today.AddDays(1);
FridayTime
= DateTime.Today.AddDays(2);
SaturdayTime
= DateTime.Today.AddDays(3);
SundayTime
= DateTime.Today.AddDays(4);
break;
case "Thursday":
week
= "星期四";
MondayTime
= DateTime.Today.AddDays(-3);
TuesdayTime
= DateTime.Today.AddDays(-2);
WednesdayTime
= DateTime.Today.AddDays(-1);
ThursdayTime
= DateTime.Today;
FridayTime
= DateTime.Today.AddDays(1);
SaturdayTime
= DateTime.Today.AddDays(2);
SundayTime
= DateTime.Today.AddDays(3);
break;
case "Friday":
week
= "星期五";
MondayTime
= DateTime.Today.AddDays(-4);
TuesdayTime
= DateTime.Today.AddDays(-3);
WednesdayTime
= DateTime.Today.AddDays(-2);
ThursdayTime
= DateTime.Today.AddDays(-1);
FridayTime
= DateTime.Today;
SaturdayTime
= DateTime.Today.AddDays(1);
SundayTime
= DateTime.Today.AddDays(2);
break;
case "Saturday":
week
= "星期六";
MondayTime
= DateTime.Today.AddDays(-5);
TuesdayTime
= DateTime.Today.AddDays(-4);
WednesdayTime
= DateTime.Today.AddDays(-3);
ThursdayTime
= DateTime.Today.AddDays(-2);
FridayTime
= DateTime.Today.AddDays(-1);
SaturdayTime
= DateTime.Today;
SundayTime
= DateTime.Today.AddDays(1);
break;
case "Sunday":
week
= "星期日";
MondayTime
= DateTime.Today.AddDays(-6);
TuesdayTime
= DateTime.Today.AddDays(-5);
WednesdayTime
= DateTime.Today.AddDays(-4);
ThursdayTime
= DateTime.Today.AddDays(-3);
FridayTime
= DateTime.Today.AddDays(-2);
SaturdayTime
= DateTime.Today.AddDays(-1);
SundayTime
= DateTime.Today;    
break;
}

表里有一个时间字段,通过这个字段获取一天时间段内的纪录。

主要用到SQL的DateDiff函数:返回两个日期之间的天数。

格式为:DATEDIFF(datepart,startdate,enddate)

要返回一天时间段内的数据SQL语句为:

select top 7 CartoonName,PlayDate,PlayTV from T_CartoonPlay where 
DateDiff(d,PlayDate,'" + SundayTime + "')=0

d代表day,天数的意思,也可以是年,月 yy,mm,整个sql语句的意思是取到指定时间SundayTime和表里的PlayDate的时间差为0的所有数据,

即返回当天的所有数据

posted @ 2011-05-24 15:55  老皮肉  阅读(1047)  评论(0编辑  收藏  举报