first day of last month condition
-
Wednesday, May 30, 2012 11:03 AM
Hi, how can I adjust this so that on the first day of the month only, it checks that Completed is after the first day of last month ( currently, it checks that Completed is after the first day of this month)
WHERE Completed >= DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0)
All Replies
-
Wednesday, May 30, 2012 11:14 AM
You can try the below (if I get your question right)
IF (SELECT DAY(GETDATE())) = 1 BEGIN SELECT DATEADD(mm, DATEDIFF(m, 0, GETDATE()),-1) END ELSE BEGIN SELECT DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0) END
Steen Schlüter Persson (DK)
-
Wednesday, May 30, 2012 11:18 AM
try below.
select dateadd(month,datediff(month,0,getdate())-1,0)
ESHANI. Please click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you
- Proposed As Answer by Satheesh Variath Wednesday, May 30, 2012 11:58 AM
- Unproposed As Answer by Naomi NMicrosoft Community Contributor, Moderator Wednesday, May 30, 2012 5:11 PM
-
Wednesday, May 30, 2012 11:58 AM
Hello,
Try this(i hope i understood your question correctly)
WHERE Completed >= DATEADD(mm, DATEDIFF(m, 0, GETDATE())-1,0)
Regards
satheesh -
Wednesday, May 30, 2012 4:35 PM
You can try the below (if I get your question right)
IF (SELECT DAY(GETDATE())) = 1 BEGIN SELECT DATEADD(mm, DATEDIFF(m, 0, GETDATE()),-1) END ELSE BEGIN SELECT DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0) END
Steen Schlüter Persson (DK)
cool, how do i use it in the where?
-
Wednesday, May 30, 2012 4:55 PM
Please try this
DATEADD(mm, DATEDIFF(M,0,GETDATE())-1,0)
to your condition this will work
-
Wednesday, May 30, 2012 5:02 PM
sorry, gives me wrong dataPlease try this
DATEADD(mm, DATEDIFF(M,0,GETDATE())-1,0)
to your condition this will work
-
Wednesday, May 30, 2012 5:04 PMModerator
declare @TestDate datetime
set @TestDate = case when datepart(day, CURRENT_TIMESTAMP) = 1
then dateadd(month, -1 + datediff(month, '19000101', CURRENT_TIMESTAMP), '19000101') else dateadd(month, datediff(month, '19000101', CURRENT_TIMESTAMP), '19000101') end select * ... where Completed >= @TestDate
For every expert, there is an equal and opposite expert. - Becker's Law
My blog- Edited by Naomi NMicrosoft Community Contributor, Moderator Wednesday, May 30, 2012 5:11 PM
- Proposed As Answer by Peja TaoModerator Thursday, May 31, 2012 6:39 AM
- Marked As Answer by Quantum Information Saturday, June 02, 2012 7:49 PM
-
Saturday, June 02, 2012 7:00 PM
Hello,
I think the condition you are expecting is anything greater than firstday of the last month.
If that is the case than try the below condition:
Completed >= DATEADD(mm, DATEDIFF(M,0,GETDATE())-1,1)
-
Saturday, June 02, 2012 7:50 PM
muchos gratias:)declare @TestDate datetime
set @TestDate = case when datepart(day, CURRENT_TIMESTAMP) = 1
then dateadd(month, -1 + datediff(month, '19000101', CURRENT_TIMESTAMP), '19000101') else dateadd(month, datediff(month, '19000101', CURRENT_TIMESTAMP), '19000101') end select * ... where Completed >= @TestDate
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
-
Thursday, July 19, 2012 10:16 AM
declare @TestDate datetime
set @TestDate = case when datepart(day, CURRENT_TIMESTAMP) = 1
then dateadd(month, -1 + datediff(month, '19000101', CURRENT_TIMESTAMP), '19000101') else dateadd(month, datediff(month, '19000101', CURRENT_TIMESTAMP), '19000101') end select * ... where Completed >= @TestDate
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
I need a multi valued table function for that right? -
Thursday, July 19, 2012 10:23 AM
that depends on your requirement. This is just a logic to find the End of month date. Can you elaborate on your requirement?
Regards
satheesh -
Thursday, July 19, 2012 10:48 AMI wish to have it in a function
-
Thursday, July 19, 2012 1:24 PMModerator
You can make it a scalar function if you'd like.For every expert, there is an equal and opposite expert. - Becker's Law
My blog- Marked As Answer by Quantum Information Thursday, July 19, 2012 2:25 PM
-
Thursday, July 19, 2012 2:25 PMah thanks!

