【问题标题】:automatically comparing the server's date from a specific range of dates from SQL table itself从 SQL 表本身的特定日期范围自动比较服务器的日期
【发布时间】:2015-10-03 22:50:01
【问题描述】:

这是我的桌子:

我有一个名为 term calendar 的表,其中包含以下列

id         = record number
schoolyear = year specificed
tstart     = term start
tend       = term end
regenroll  = advance enrollment
regend     = end of advance enrollment
nenroll    = normal enrollment
nend       = end of normal enrollment
ienroll    = late enrollment
iend       = end of late enrollment

我的 SQL 表中的日期格式为 DateTime。

我想要发生的是,当用户登录时第一次调用该操作时。服务器当前的DateTime 将用于比较它们是否在tstart 和tend 的范围内和我会从中得到schoolyear 和term。

现在我的问题是,我的 sql/linq 查询应该是什么?

我之前研究过DateTime,我发现的示例依赖于where 子句和一组预先指定的范围,但我的范围来自数据库本身。我需要重新设计我的桌子吗?

【问题讨论】:

    标签: c# sql-server asp.net-mvc linq datetime


    【解决方案1】:

    此查询应该满足您的要求:

    DECLARE @checkDate DATETIME
    
    SET @checkDate = GETDATE();
    
    SELECT tc.schoolyear ,
           tc.term
    FROM termcalendar tc
    WHERE
           tc.start <= @checkDate
           AND tc.tend >= @checkDate
    

    您应该注意不返回任何记录的期间,例如 2015-04-02。

    【讨论】:

    • 我复制粘贴了你的代码并尝试运行它,这是我得到的Incorrect syntax near the keyword 'end'.
    • 我的字段名称有误。它应该是 tend 而不是 end。我更正了查询,
    • 哦,我明白了,好吧。如果我决定设置时间,它还是一样吗?
    • 是的。我将时间设置为变量以确保它是相同的。
    • 我计划迟早把时间放到2015-08-07 12:00:00.. 这样的地方。我需要更改您的查询中的任何内容吗?
    猜你喜欢
    • 1970-01-01
    • 2014-10-28
    • 2010-09-13
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多