What do you think will be the output?
DECLARE @d datetime
SET @d = '20071010'
SELECT DATEADD(yy, DATEDIFF(yy, 0, @d)+1, -1)
After that run this to see how to get first and last days for years, quarters, months and weeks(be careful of ISO weeks!)
DECLARE @d datetime
SET @d = '20071010'
SELECT DATEADD(yy, DATEDIFF(yy, 0, @d), 0) as FirstDayOfYear,
DATEADD(yy, DATEDIFF(yy, 0, @d)+1, -1) as LastDayOfYear,
DATEADD(qq, DATEDIFF(qq, 0, @d), 0) as FirstDayOfQuarter,
DATEADD(qq, DATEDIFF(qq, 0, @d)+1, -1) as LastDayOfQuarter,
DATEADD(mm, DATEDIFF(mm, 0, @d), 0) as FirstDayOfMonth,
DATEADD(mm, DATEDIFF(mm, 0, @d)+1, -1) as LastDayOfMonth,
DATEADD(wk, DATEDIFF(wk, 0, @d), 0) as FirstDayOfWeek,
DATEADD(wk, DATEDIFF(wk, 0, @d)+1, -1) as LastDayOfWeek
Wednesday, October 10, 2007
SQL Teaser: Guess the output
Posted by Denis at 10:50 AM
Labels: Dates, Datetime, SQL Server 2000, SQL Server 2005, Teaser
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment