且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果等于或小于今天SQL Server CE,则从表中获取expiredate

更新时间:2022-11-01 12:39:50

从发布的代码中,我假设您正在尝试获取ExpireDate小于或等于当前日期加上警报日数的记录数。如果是这种情况,请将查询修改为如下所示,它应该有效。

 选择计数(*)来自 tblitem 其中 dateadd(day,AlertDays,getdate())< = ExpireDate 


Some help would be appreciated.
I have a table tblItem with columns ExpireDate (DateTime), AlertDays (Int).
Each Item I insert has an expiredate, I want to be alert in x days before the expiredate.
I want to count all the items where today + alertdays <= ExpireDate.
I have the following code but it is causing an error:

There was an error parsing the query. [ Token line number = 1,Token line offset = 48,Token in error = select ]



internal static int getAlertDateExpire()
{
    SqlCeConnection con = ConectionAndData.Con;

    if (con.State == System.Data.ConnectionState.Closed)
        con.Open();

    int number = 0;

    string sqlCommand = "select count(*) from tblItem where (Getdate()+(select AlertDays from tblItem)) As today <= ExpireDate";

    SqlCeCommand com = new SqlCeCommand(sqlCommand, con);

    try
    {
        number = (Int32)com.ExecuteScalar();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        con.Close();
    }

    return number;
}



What I have tried:

string sqlCommand = "(select count(*) from tblItem where DATEADD(day, (select AlertDays from tblItem) ,Getdate()) <= ExpireDate)";

From the code posted, I assume you are trying to get the count of records having ExpireDate less than or equal to current date plus number of alertdays. If that is the case, modify the query to something like below and it should work.
select count(*) from tblitem where dateadd(day, AlertDays,getdate()) <= ExpireDate