April 30, 2008 at 12:56 am
SELECTDATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '18991231') AS LastOfPreviousMonth,
DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101') AS FirstOfCurrentMonth,
DATEADD(MONTH, DATEDIFF(MONTH, '18991231', GETDATE()), '18991231') AS LastOfCurrentMonth,
DATEADD(MONTH, DATEDIFF(MONTH, '18991231', GETDATE()), '19000101') AS FirstOfNextMonth
N 56°04'39.16"
E 12°55'05.25"
April 30, 2008 at 12:57 am
And with integers as parameter values
SELECTDATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), -1) AS LastOfPreviousMonth,
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AS FirstOfCurrentMonth,
DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()), -1) AS LastOfCurrentMonth,
DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()), 0) AS FirstOfNextMonth
N 56°04'39.16"
E 12°55'05.25"
April 30, 2008 at 5:40 am
Jeff,
Really Great!
(No character based date conversions) + (No concatenation) = Very high speed short code.
You taught me a new concept.
Thanks a lot for your prompt reply with good explanations !
But i am getting the error message while executing the code. Yes.
DATEDIFF(mm,0,@DateStart)
When i replace '0' with some other date it is giving the result.
If i leave as it is, it is showing like
DATEDIFF function is invoked with wrong parameter(s).
Please correct me where i am doing mistake.
In the meantime, please try to figure out what dateformat you'd like for startdate and enddate... I recommend not using one at all.
Apr/01/2007
Sorry As midnight time was here i am unable to see your reply.I saw it today morning only.
karthik
April 30, 2008 at 8:53 am
The code I posted work without error on my end. If you are typing, are you sure that your typing a zero and not an "OH"?
You know the rules... post the code you're having a problem with... 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 16 through 18 (of 18 total)
You must be logged in to reply to this topic. Login to reply