Get first day of previous month in YYYY-MM-DD format - sql

How do I get the first day of previous month in yyyy-mm-dd format.
I have tried this
SELECT CONVERT(VARCHAR(10), DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0)), 120)

You can use the EOMONTH function for this. This function is supposed to return the end of month so add one day to result:
SELECT DATEADD(DAY, 1, EOMONTH(GETDATE(), -2)) -- returns 2019-07-01 on 2019-08-28 01:02:03.004
Wrap the above expression inside FORMAT(..., 'yyyy-MM-dd') to format the result.

This will work:
SELECT CONVERT(VARCHAR(10),DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)),23)
Output:
2019-07-31

Related

Using DATEADD and DATEDIFF truncates the time

I am using the following to get to get the 1st day of next month with time:
select DATEADD(m, DATEDIFF(m, -1, getdate()), 0)
But the output is:
2018-12-01 00:00:00.000
And the expected result is:
2018-12-01 11:53:30.677
I have tried various approaches but not able to get required output. I am using SQL Server 2008.
You can add two datetime values, one for the date and the other for the time:
select DATEADD(month, DATEDIFF(month, -1, getdate()), 0) + cast(cast(getdate() as time) as datetime)
I am guessing that you want the time value from the current time.
Subtract DAY(#date) - 1 days from #date to get first day of that month including time. Then add one month:
SELECT DATEADD(MONTH, 1, DATEADD(DAY, -DAY(GETDATE()) + 1, GETDATE()))
-- 2018-12-01 04:52:33.403
try like below
select DATEADD(m, DATEDIFF(m, -1, getdate()), 0)+ convert(DATETIME,'11:53:30.677')
in case of current time it would be like below
select DATEADD(m, DATEDIFF(m, -1, getdate()), 0)+ convert(datetime, convert(time,getdate()))

SQL Server finding end date of the month - month range

In my SQL Server procedure, I have one input parameter like created_date.
Based on that created date I have to find out first and last date of the month.
For example,
If created_date is 12-08-2016,
start_date should be '01-08-2016'
end_date should be '31-08-2016'
If Created_date is 15-06-2016
start_date should be '01-06-2016 00:00:00'
end_date should be '30-06-2016 23:59:59'
Since your input is in DD-MM-YYYY format, so it is need to covert first as MM-DD-YYYY then only it is easy to find the first and last day of the month.
The below query will accept the input and return the result in your expected format:
DECLARE #datevalue AS VARCHAR(20) = '15-06-2016'; --12-08-2016';
SELECT CONVERT(VARCHAR(10), DATEADD(M, DATEDIFF(M, 0, CONVERT(DATETIME, #datevalue, 105)), 0), 105) [start_date],
CONVERT(VARCHAR(10), DATEADD(D, -1, DATEADD(M, DATEDIFF(m, 0, CONVERT(DATETIME, #datevalue, 105)) + 1, 0)), 105) [end_date]
Output:
start_date end_date
----------------------
01-06-2016 30-06-2016
select
DATEADD(Month, DATEDIFF(Month, 0, GETDATE()), 0) as monthfirstdate,
CAST(EOMONTH(GETDATE()) as DATETIME) as monthlastdate
EOMONTH works from SQL server 2012

Get Last Day of the Month with the time set to 00:00:00 on MSSQL

I had a query and I am trying to get the yesterday date at 00:00:00 and the previous month date at 00:00:00.
This is my query:
SELECT DATEADD( DD,-2, CONVERT( CHAR(8) , getdate() , 112 )) 'Yesterday',
CONVERT( CHAR(8) ,DATEADD(ms,-3, DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))) , 112 ) 'Last Day of Last Month'
The result is as follows:
Yesterday:2015-05-24 00:00:00.000
Last Day of Last Month:20150430
What is wrong here? Even if I move the convert I still getting this format. Is my approach correct?
Try this:
select
dateadd(dd, datediff(dd, 0, getdate()) - 1, 0) as yesterday,
dateadd(dd, -1, dateadd(mm, datediff(mm, 0, getdate()), 0)) as lastDayOfLastMonth
For more common date routines, visit this article by Lynn Pettis
If you're using SQL Server 2012 or above, you can simply use EOMONTH() built-in function.
SELECT CAST(EOMONTH (CURRENT_TIMESTAMP, -1) AS DATETIME) will return you 2015-04-30 00:00:00.000

How to get 00:00:00 in datetime, for First of Month?

I wrote a query to obtain First of month,
SELECT DATEADD(DAY, -(DATEPART(DAY,GETDATE())-1), GETDATE()) as First_Of_Month;
for which i do get the appropriate output, but my time stamp shows the current time.
Here's what I am doing in the query, hope i make sense.
using datepart i calculated the no. of days (int) between the 1st and today (27-1 =26)
Then using dateadd function, i added "-datepart" to get the first of the month.
This is just changing the date, what should i look at or read about in order to change the time. I am assuming that it would have something to do with 19000101
For SQL Server 2012 (thanks adrift and i-one)
DECLARE #now DATETIME = CURRENT_TIMESTAMP;
SELECT DATEADD(DAY, 1, EOMONTH(#now, -1));
-- or
SELECT DATEFROMPARTS(YEAR(#now), MONTH(#now), 1);
For SQL Server 2008+
DECLARE #now DATETIME = CURRENT_TIMESTAMP;
-- This shorthand works:
SELECT CONVERT(DATE, #now+1-DAY(#now));
-- But I prefer to be more explicit, instead of relying on
-- shorthand date math (which doesn't work in all scenarios):
SELECT CONVERT(DATE, DATEADD(DAY, 1-DAY(#now), #now));
For SQL Server 2005
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()),0);
A caveat if you're using this SQL Server 2005 method: you need to be careful about using expressions involving DATEDIFF in queries. SQL Server can transpose the arguments and lead to horrible estimates - as seen here. It might actually be safer to take the slightly less efficient string approach:
SELECT CONVERT(DATETIME, CONVERT(CHAR(6), GETDATE(), 112) + '01');
SELECT convert(varchar(10),DATEADD(DAY, - (DATEPART(DAY,GETDATE())-1), GETDATE()),120)+' 00:00:00' as First_Of_Month;
Just the date
DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
So for a month it is:
DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) AS FirstDatetimeOfMonthmm,
I think the easiest way is to cast the result to date:
SELECT cast(DATEADD(DAY, -(DATEPART(DAY,GETDATE())-1), GETDATE()) as date) as First_Of_Month
One alternative:
SELECT cast(
cast(datepart(yyyy, getdate()) as varchar)
+ '-'
+ cast(datepart(mm, getdate()) as varchar) + '-01 00:00:00'
as datetime)
Build up the date from year/month components, then tack on the 1st and midnight.
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) as First_Of_Month;
Try following code:
SELECT CAST(CAST(GETDATE() AS DATE) AS DATETIME) AS StartDateTime,
DATEADD(ms, -3, CAST(CONVERT(date, DATEADD (DAY,1,GETDATE())) AS varchar(10))) AS EndDateTime
Result:
StartDateTime
EndDateTime
2022-05-23 00:00:00.000
2022-05-23 23:59:59.997

How to calculate a date after 1 month currentDate

How can I calculate a date after a month of current date.
Example if I have 19/04/2012 I want to get 19/05/2012. Is there any function on sql that allows doing this?
I need to do the same for a year also like 19/04/2012 and set 19/04/2013
Cheers
You can use
DATEADD (datepart , number , date )
as specified here
Examples (thanks to DarrenDavis)
for month:
select dateadd(m, 1, getdate())
for year:
select dateadd(YY, 1, getdate())
Use the dateadd() function
SELECT dateadd(mm, 1, '19-Apr-2012')
To add 1 month to the current date:
SELECT DATEADD(m, 1, GETDATE())
To add 1 year to the current date:
SELECT DATEADD(YY, 1, GETDATE())
For additional arguments see:
http://msdn.microsoft.com/en-us/library/ms186819.aspx
Something like this should do the job
SELECT DATEADD(month, 1, '2012-04-19')
for a year
SELECT DATEADD(year, 1, '2012-04-19')
http://msdn.microsoft.com/en-us/library/ms186819.aspx
You can use DateAdd:
select dateadd(m, 1, getdate())
select dateadd(yy, 1, getdate())
search online for other period indicators such as hour, minute, etc!
You can use this query for 1 month ahead date . This query works fine in oracle.
select add_months(trunc(sysdate),1) from dual;
For 1 year ahead date use this query
select add_months(trunc(sysdate),12) from dual;