如果您正在使用MSSQL你必須BETWEEN運算這是完美的:
SELECT EmployeeID, DriverLicenseExpiryDate AS ExpiryDate, 'Driver License' AS LicenseType
FROM YourTable
WHERE DriverLicenseExpiryDate BETWEEN GETDATE() AND GETDATE() + 30
UNION SELECT EmployeeID, AutoInsuranceExpiryDate AS ExpiryDate, 'Auto Insurance' AS LicenseType
FROM YourTable
WHERE AutoInsuranceExpiryDate BETWEEN GETDATE() AND GETDATE() + 30
UNION SELECT EmployeeID, TWICCardExpiryDate AS ExpiryDate, 'TWIC Card' AS LicenseType
FROM YourTable
WHERE TWICCardExpiryDate BETWEEN GETDATE() AND GETDATE() + 30
如果你在它允許您可以簡化這種使用變量的腳本的環境中工作的奢侈品:
DECLARE @startDate AS DATE = GETDATE();
DECLARE @endDate AS DATE = GETDATE() + 10;
SELECT EmployeeID, DriverLicenseExpiryDate AS ExpiryDate, 'Driver License' AS LicenseType
FROM YourTable
WHERE DriverLicenseExpiryDate BETWEEN @startDate AND @endDate
UNION SELECT EmployeeID, AutoInsuranceExpiryDate AS ExpiryDate, 'Auto Insurance' AS LicenseType
FROM YourTable
WHERE AutoInsuranceExpiryDate BETWEEN @startDate AND @endDate
UNION SELECT EmployeeID, TWICCardExpiryDate AS ExpiryDate, 'TWIC Card' AS LicenseType
FROM YourTable
WHERE TWICCardExpiryDate BETWEEN @startDate AND @endDate;
類似'select * from table where expirydate <= now()+ 30'只需替換爲DBMS的正確語法 –
這不僅僅是選擇員工1次嗎? –
它選擇所有過期日期小於+30天的僱員 –