Tuesday, February 27, 2007

T-SQL select next day

In MSSQL Server 2000 and 2005, run the following script:

declare @date datetime
set @date = '02/28/2007'

select @date+1


Run it, and it will give '03/01/2007'! Moreover, set @date to '02/28/2008', and it will give '02/29/2008'! To definitely make sure it's all working fine, set the @date to be '12/31/2007', run the query, and the answer will be '01/01/2008'.

When I provide the date in MM/DD/YYYY format to a query or stored procedure, it interprets it as MM/DD/YYYY 12:00:00 AM, beginning of the day. So when I need to make an inclusive search on the date, I need to provide date+1 value of the parameter. Instead of creating complicated clauses, @datetime+1 simply works.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home