员工加入日期和离开日期之间的数据的SQL

时间:2019-02-27 07:20:52

标签: sql sql-server

我有一位员工参加了两次会议。请查看下表。

EmpID    EmpName    DateOFJoin    DateOfLeaving    Status
1         XYZ       2015-10-01    2017-09-26       De-Active
2         ABC       2018-01-01                     Active                      
3         XYZ       2018-10-15                     Active     

我想要输出像 例如, 我有FromDate和ToDate,例如“ 2019-12-01”和“ 2019-12-31”

EmpID    EmpName    DateOFJoin    DateOfLeaving    Status
2         ABC       2018-01-01                     Active 
3         XYZ       2018-10-15                     Active 

如果我有FromDate和ToDate,例如“ 2017-08-01”和“ 2017-09-30”

EmpID    EmpName    DateOFJoin    DateOfLeaving    Status
1         XYZ       2015-10-01    2017-09-26       De-Active

如果我有FromDate和ToDate,例如“ 2018-01-01”和“ 2018-03-31”

EmpID    EmpName    DateOFJoin    DateOfLeaving    Status
2         ABC       2018-01-01                     Active  

请帮助为其准备SQL。

4 个答案:

答案 0 :(得分:2)

使用coalesce()功能

DEMO

select EmpID,EmpName,DateOFJoin,DateOfLeaving,Status
from tablename
where DateOFJoin<=param1 and coalesce(DateOfLeaving,param2)>=param2

答案 1 :(得分:0)

您可以在此过程中尝试以下方法:

class TabBarViewController: UITabBarController {

override func viewDidLoad() {

}

override func viewDidAppear(_ animated: Bool) {
    lockAppCallBack()
}

func lockAppCallBack() {
    let callBack = {
        let myModalViewController = R.storyboard.authentication.authenticationViewController()
        ScreenLocker.isAutoLocked = true
        myModalViewController!.modalPresentationStyle = UIModalPresentationStyle.fullScreen
        myModalViewController!.modalTransitionStyle = UIModalTransitionStyle.coverVertical
        self.present(myModalViewController!, animated: true, completion: nil)
    }
    AuthenticationPatternPresenter.initScreenLocker(callBack: callBack)
}

答案 2 :(得分:0)

您可以使用此:

SELECT * 
FROM table_name
WHERE DateOFJoin <= ToDate AND COALESCE(DateOfLeaving, FromDate) >= FromDate;

答案 3 :(得分:0)

尝试将WHEREISNULL()一起使用:

SELECT 
* 
FROM yourTable t
WHERE T.DateOfJoin <=@FromDate AND ISNULL(T.DateOfLeaving, @ToDate)>=@ToDate

让我展示一个例子:

DECLARE @table TABLE 
(
EmpID int, 
EmpName varchar(50),    
DateOfJoin datetime,
DateOfLeaving DATETIME,
StatusName VARCHAR(50)
)

INSERT INTO @table
(
    EmpID,
    EmpName,
    DateOfJoin,
    DateOfLeaving,
    StatusName
)
VALUES
 (1, 'XYZ',      '2015-10-01',    '2017-09-26', 'De-ACTIVE')
,(2, 'ABC',      '2018-01-01',    NULL, 'ACTIVE')                      
,(3, 'XYZ',      '2018-10-15',    NULL, 'ACTIVE')    


DECLARE   @FromDate  VARCHAR(50) = '2019-12-01'
        , @ToDate  VARCHAR(50) = '2019-12-31'
SELECT 
* 
FROM @table t
WHERE T.DateOfJoin <=@FromDate AND ISNULL(T.DateOfLeaving, @ToDate)>=@ToDate