February 11, 2004 at 5:26 pm
I'm trying to find the 1st day of next month and I was trying to enhance Michael Rosquist routines. He put two routines together, one that returns the last day of the previous month and another one that returns the first day of current month.
SELECT DATEADD(dd, CAST(DATEDIFF(DAY,'17530101',CURRENT_TIMESTAMP) AS int)
- DAY(CURRENT_TIMESTAMP), '17530101')
as LastDayPreviousMonth
SELECT DATEADD(dd, CAST(DATEDIFF(DAY,'17530101',CURRENT_TIMESTAMP) AS int)
- DAY(CURRENT_TIMESTAMP) + 1, '17530101')
as FirstDayCurrentMonth
Anyone have any suggestions on finding the first day of the next month??
February 11, 2004 at 5:42 pm
February 11, 2004 at 6:57 pm
SELECT CAST(LEFT(GETDATE()-DAY(GETDATE()),11) AS datetime) LastDayLastMo,
CAST(LEFT(GETDATE()-DAY(GETDATE())+1,11) AS datetime) FirstDayThisMo,
CAST(LEFT(DATEADD(m,1,GETDATE()-DAY(GETDATE())+1),11) AS datetime) FirstDayNextMo
--Jonathan
February 11, 2004 at 7:02 pm
I prefer this method.
SELECT
DATEADD(d,-1,DATEADD(m,datediff(m,0,getdate()),0)) as LastDayPreviousMonth,
DATEADD(m,datediff(m,0,getdate()),0) as FirstDayOfCurrentMonth,
DATEADD(m,1,DATEADD(m,datediff(m,0,getdate()),0)) as FirstDayOfNextMonth
February 11, 2004 at 10:55 pm
We can try this with time
select dateadd(dd, -day(getdate()), getdate()) as Last_Day_Previous_Month,
dateadd(dd, -day(getdate())+1, getdate()) as First_Day_Current_Month,
dateadd(mm, 1, dateadd(dd, -day(getdate())+1, getdate())) as First_Day_Next_Month
Thanks
Mohit Nayyar
http://mohitnayyar.blogspot.com/
"If I am destined to fail, then I do have a purpose in my life, To fail my destiny"
February 11, 2004 at 11:01 pm
http://www.geocities.com/sqlserverexamples/date5.htm
Goood site too
Declare @Fdaynm datetime -- First Day of Next Month
-- Set the @Fday to the first day of the next month
-- By adding a month to the current date and then
-- subtracting the number of days from the calculated date next month and then add 1
-- This keeps the existing time
Set @Fdaynm = dateadd(day,-1*(day(getdate())-1),dateadd(month,1,getdate()))
-- Print the @Fdaynm
select @Fdaynm
-- Another Way although this sets the time to 00:00:00.000
set @Fdaynm = cast(
cast(datepart(year,dateadd(month,1,getdate())) as char(4)) + '-' +
cast(datepart(mm,dateadd(month,1,getdate())) as char(2)) + '-01'
as datetime)
select @Fdaynm
My Blog:
February 12, 2004 at 9:33 am
Here are two sql statements. The first when will return the date as mm/dd/yyyy. The second will return the day of the week such as Monday. 1) SELECT CONVERT(varchar(2), MONTH(DATEADD(month, 1, GETDATE()))) + '/1/' + CONVERT(char(4), YEAR(DATEADD(month, 1, GETDATE()))) 2) SELECT DATENAME(weekday, CONVERT(varchar(2), MONTH(DATEADD(month, 1, GETDATE()))) + '/1/' + CONVERT(char(4), YEAR(DATEADD(month, 1, GETDATE()))))
February 12, 2004 at 4:24 pm
Thanks for all the help...
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply