• 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的所有数据,

    即返回当天的所有数据

  • 相关阅读:
    Java线程池
    代理模式
    Bean的加载
    Spring的启动流程
    MFC编程入门之二十七(常用控件:图片控件PictureControl)
    MFC编程入门之二十六(常用控件:滚动条控件ScrollBar)
    MFC编程入门之二十五(常用控件:组合框控件ComboBox)
    MFC编程入门之二十四(常用控件:列表框控件ListBox)
    MFC编程入门之二十三(常用控件:按钮控件的编程实例)
    MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)
  • 原文地址:https://www.cnblogs.com/sheseido/p/2055641.html
Copyright © 2020-2023  润新知