如果等于或小于今天 SQL 服务器 CE,则从 table 获取过期日期

Getting expiredate from table if equal or less then today SQL Server CE

一些帮助将不胜感激。

我有一个 table tblItemExpireDate (DateTime), AlertDays (Int).

我插入的每个项目都有一个 expiredate,我想在 expiredate 之前的 x 天内保持警惕。

我想统计 today + alertdays <= ExpireDate 处的所有项目。

我有以下代码,但它导致了错误:

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 Order By ItemName ASC";

    SqlCeCommand com = new SqlCeCommand(sqlCommand, con);

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

    return number;
}

您不能仅使用 + 添加天数,请使用 DATEADD 函数:

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

我得到了帮助,想分享答案。

(select count(*) from tblItem where dateadd(day,AlertDays,getdate()) >= ExpireDate)