具有大小写条件的日期过滤器SQL Server

时间:2019-03-07 16:50:31

标签: sql-server-2012

我有2个表,其中有一个记录的记录如下

CREATE TABLE Student
(
    StudentId INT,
    StudentName NVARCHAR(200)
)

CREATE TABLE StudentHist
(
    StudentHistId INT,
    BranchId INT,
    StudentId INT,
    ExitDate DATE
)

INSERT INTO Student 
VALUES (1, 'ABC'), (2, 'PQR')

INSERT INTO StudentHist 
VALUES (1, 1, 1, null), (2, 2, 1, '2016-01-01'),
       (3, 2, 2, '2017-01-01')

SELECT S.StudentName
FROM Student S 
INNER JOIN StudentHist SH ON S.StudentId = SH.StudentId
                          AND (SH.ExitDate IS NULL OR SH.ExitDate >= getdate())
                          AND SH.BranchId = 2

当前我有一个查询,该查询给出退出日期为null的记录,我需要的是切换条件,它将检查日期为null并执行当前代码(如果未通过histid获取最新信息并获取日期< / p>

https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=1036cec8a82566534665c89f7279ebcb

0 个答案:

没有答案