to display message based on start date and stop date
I need to retrieve a particular message based on start date and stop date. the table consists of rows of messages each assigned for particular start date and stop date. the sql should compare the current date with start date and stop date and if its matches start date it should display only that particular开发者_如何学Go message. that message should not be displayed when current date exceeds stop date
Assuming table
Column Name | Type
StartDate | DateTime
StopDate | DateTime
Message | no matter
SQL:
-- cache to optimize performance
DECLARE @currentDate DateTime
SET @currentDate = GETDATE()
SELECT message
FROM dataTable
WHERE @currentDate < StopDate AND @currentDate = StartDate
An example
declare @date datetime
select @date = getdate()
declare @table table (FromDate datetime, ToDate datetime, mesg varchar(50))
insert @table values('2008','12-31-2010', 'once')
insert @table values('01-01-2011','12-31-2011', 'this year')
insert @table values('01-01-2012','12-31-2015', 'future')
select mesg from @table where @date between FromDate and ToDate
精彩评论