datetime - Current week in current month showing wrong in SQL Server -
this how july month looks like.
i trying pick current week in current month getdate()
parameter using below code.
declare @date datetime = getdate() select datepart(day, datediff(day, 0, @date) / 7 * 7) / 7 + 1
expecting result
date -> week number 2015-07-01 -> 1 2015-07-06 -> 2 2015-07-13 -> 3
but, instead, result is:
date -> week number 2015-07-01 -> 5 2015-07-06 -> 1 2015-07-13 -> 2
how first format in sql server?
thanks in advance
this seems right answer
select datediff(week, dateadd(week, datediff(week, 0, dateadd(month, datediff(month, 0, @date), 0)), 0), @date)+1
Comments
Post a Comment