I have one table that contains StartDate and EndDate (Datetime)
I'm looking for a nice query in SQL Server 2008R2 that will check if StartDate and EndDate are equal and if they are, change another value to '0'
In short, my program has "Learning Environments," each with a status code (open/closed/archived). At night I'm running a few scheduled procedures and if I set the opening and ending date to the same date, the status should change to be closed.
So the query should:
Sounds like a simple UPDATE
statement:
UPDATE table
SET status = 0
WHERE CAST(StartDate AS DATE) = CAST(EndDate AS DATE)
AND CAST(StartDate AS DATE) = CAST(GETDATE() AS DATE)
I like using DateAdd and DateDiff since it's always been a little more efficient than other techniques.
Working sqlfiddle: http://sqlfiddle.com/#!6/f1a76/1
UPDATE Table1
SET Status = 0
WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, [StartDate])) = DATEADD(dd, 0, DATEDIFF(dd, 0, [EndDate]))
AND DATEADD(dd, 0, DATEDIFF(dd, 0, [StartDate])) = DATEADD(dd, 0, DATEDIFF(dd, 0, GetDate()))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.