Good link for all about datetime in sql server
Friday, February 25, 2011
All About Date Time in sql server
http://www.databasejournal.com/features/mssql/article.php/2191631/Working-with-SQL-Server-DateTime-Variables.htm
Get Starting of Current Week, Current Month, Current Year in Sql Server
declare @CurrentWeek date, @CurrentMonth date, @Year date, @LastWeek date, @LastMonth date, @LastYear date
Declare @DateValue DateTime = '3/24/2011'
select @CurrentWeek = DATEADD (wk, datediff( wk, 6, @DateValue ), 6 )
select @LastWeek = DATEADD(Wk, -1, (@CurrentWeek))
select @CurrentMonth = CONVERT(date, CONVERT(varchar, DatePart (M, @DateValue)) + '/1/' + convert(varchar, DATEPART(YEAR,@DateValue)))
select @LastMonth = CONVERT(date, DATEADD(M,-1 , @CurrentMonth), 106)
select @Year = CONVERT(date, CONVERT(varchar, '1/1/' + convert(varchar,DATEPART(YEAR,@DateValue))))
select @LastYear = CONVERT(date,DATEADD(YEAR, -1,@Year),106)
select @CurrentWeek,@LastWeek,@CurrentMonth,@LastMonth, @Year, @LastYear
Subscribe to:
Posts (Atom)