Using SQL Server 2000, how can I get the first and last date of the current year?
Expected Output:
01/01/2012 and 31/12/2012
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The above query gives a datetime value for midnight at the beginning of December 31. This is about 24 hours short of the last moment of the year. If you want to include time that might occur on December 31, then you should compare to the first of the next year, with a
<comparison. Or you can compare to the last few milliseconds of the current year, but this still leaves a gap if you are using something other than DATETIME (such as DATETIME2):Other Periods
This approach has two nice aspects: good performance and it can easily be changed for other periods by replacing both occurrences of
yy(=year) with a different string:(Be careful of weeks: the starting day depends on server settings.)
Tech Details
This works by figuring out the number of years since 1900 with
DATEDIFF(yy, 0, GETDATE())and then adding that to a date of zero = Jan 1, 1900. This can be changed to work for an arbitrary date by replacing theGETDATE()portion or an arbitrary year by replacing theDATEDIFF(...)function with "Year – 1900."