How to minus 2 month in SQL Server - sql

My requirement is to make this query :
SELECT DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)
Minus by 2 months and the date should be stayed in 1.

Add DATEADD function on top of your existing query
SELECT DATEADD(MONTH,-2,DATEADD(M, DATEDIFF(M, 0, GETDATE()), 0)) --2016-05-01 00:00:00.000
If you are using SQL SERVER 2012+ the use EOMONTH function
SELECT DATEADD(DAY,1,EOMONTH(GETDATE(),-3)) --2016-05-01

In SQL Server, the following gets the first day of this month:
select cast(dateadd(day, 1 - day(getdate()), getdate()) as date)
For last month:
select dateadd(month, -1, cast(dateadd(day, 1- day(getdate()), getdate()) as date))
In SQL Server 2012+, you can also do:
select dateadd(month, -2, datefromparts(year(getdate()), month(getdate()), 1)
The use of adding months to a zero time is a hack before SQL Server had better date time functions.

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()))

Filter results after date SQL Server

I need to filter my query to extract the results between 25th of the previous month and 25th of the current month. The SP receives a date as parameter, which I am using to extract the current month.I tried to make some casting but I can't figure how to deal with the January month when the last month has a different year(there must be a more efficient way also)
#Date smalldatetime --received as parameter
select *
from mytable
where mytable.mydate between '25/'+cast(MONTH(#date)-1 as varchar(2))+'/'+cast(YEAR(#date) as varchar(4)) and '25/'+cast(MONTH(#date) as varchar(2))+'/'+cast(YEAR(#date) as varchar(4))
I would just do:
select t.*
from t
where mydate >= dateadd(month, -1, datefromparts(year(getdate()), month(getdate()), 25)) and
mydate < datefromparts(year(getdate()), month(getdate()), 25)
I would use datefromparts() rather than trying to construct a date string for this purpose.
You don't need to do any casting. SQL Server's datetime functions are quite versatile. Try this:
SELECT *
FROM mytable
WHERE mytable.mydate BETWEEN DATEADD(DAY, 25, DATEADD(MONTH, DATEDIFF(MONTH, 0, #Date) - 1, 0)) AND DATEADD(DAY, 25, DATEADD(MONTH, DATEDIFF(MONTH, 0, #Date), 0))

T-SQL syntax to filter records where the datetime variable is greater than or equal to the 1st Day of the Current Month

I am using SQL Server 2014 and I need to add a line of code to my SQL query that will filter the data extracted only to those records where the StayDate (a column in database) is greater than or equal to the 1st day of the current month.
In other words, the line of code I need is the following:
WHERE StayDate >= '1st Day of Current Month'
Note: StayDate is in the datetime format (eg: 2015-12-18 00:00:00.000)
Use EOMONTH to get the first day of current month
WHERE StayDate >= Dateadd(dd, 1, Eomonth(Getdate(), -1))
SQL Server 2012 and above
WHERE StayDate >= DATEADD(DAY, 1, EOMONTH(GETDATE(), -1))
Before SQL Server 2012
WHERE StayDate >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
You can try following approach:
declare #t datetime = '2016-02-28'
select DATEADD(D, -DATEPART(d, #t) + 1, #t)
So in your case:
WHERE StayDate >= DATEADD(D, -DATEPART(d, GETDATE()) + 1, GETDATE())
StayDate >= DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')
One other option; since staydate being on or after (greater than or equal to) the first day of the current month is equivalent to searching for staydates during the current month:
where datepart(mm, staydate) = datepart(mm, getdate())
and datepart(yyyy, staydate) = datepart(yyyy, getdate())

Between Dates Where Clause With Shifting Date

Is it possible to do the following in SQL. I would like to write a code that will return records between certain dates, but will automatically update when the report runs. So for example:
on 12/18 I would like to return records from 11-1 to 11/18 and 12/1 to 12/18 so I can compare month over month on these records in my report.
I understand that I can do:
WHERE
[database] BETWEEN '2013-11-01' and DATEADD(Month, 1, getdate ()))
and not [database] IN ('2013-11-19 and '2013-11-30')
but I will have to go into the query everyday to update the not between. I would like to make it so that I can run the query on any day and get the records from the previous month up until the date match for this month. I currently have been working with this:
B.[datepaid] between DATEADD(Month, -1, getdate() )
and DATEADD(MONTH, 1, getdate() ))
but it is returning all records from November.
I would recommend against using BETWEEN with Dates as it can have some unexpected results. But in your case you can use:
WHERE ([Date] >= DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()) - 1, '19000101')
AND [Date] < DATEADD(MONTH, -1, GETDATE()))
OR ([Date] >= DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')
AND [Date] < GETDATE());
Demo on SQL Fiddle

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