Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DATEDIFF Getting the previous month

Tags:

sql

sql-server

I want to get the previous month relative to the current date

SELECT datediff(mm,-1,2-2-2011)

This query gives 67 which is a wrong value .. where i went wrong ?

like image 986
Sudantha Avatar asked Feb 12 '11 09:02

Sudantha


People also ask

How do I get previous month in SQL?

select * from orders where order_date>now() - interval 1 month; In the above query, we select rows after past 1 month interval. We use INTERVAL clause and NOW() function to obtain the date 1 month in the past, from present date.

How do I get a datediff between two dates?

To find the difference between dates, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument defines the part of the date/datetime in which you'd like to express the difference. Its value can be year , quarter , month , day , minute , etc.

Does datediff include start and end?

Description. DATEDIFF(datePart,startDate,endDate) returns an INTEGER value that is the difference between the starting and ending date (startDate minus endDate) for the specified date part (seconds, days, weeks, and so on).

Can datediff be used in a where clause?

Note: DATEADD and DATEDIFF SQL function can be used in the SELECT, WHERE, HAVING, GROUP BY and ORDER BY clauses.


1 Answers

You can use DATEADD

eg.

SELECT DATEADD(month, -1, GETDATE())
like image 71
Pure.Krome Avatar answered Oct 06 '22 18:10

Pure.Krome