how to get the 30 days before date from Todays Date

SqlSql ServerDatetime

Sql Problem Overview


How do you get the 30 days before today in SQL.

Sql Solutions


Solution 1 - Sql

T-SQL

declare @thirtydaysago datetime
declare @now datetime
set @now = getdate()
set @thirtydaysago = dateadd(day,-30,@now)

select @now, @thirtydaysago

or more simply

select dateadd(day, -30, getdate())

(http://msdn.microsoft.com/en-us/library/ms186819.aspx">DATEADD on BOL/MSDN)

MYSQL

SELECT DATE_ADD(NOW(), INTERVAL -30 DAY)

(http://www.electrictoolbox.com/date-add-mysql-intervals-dates/"> more DATE_ADD examples on ElectricToolbox.com)

Solution 2 - Sql

In MS SQL Server, it is:

SELECT getdate() - 30;

Solution 3 - Sql

SELECT (column name) FROM (table name) WHERE (column name) < DATEADD(Day,-30,GETDATE());

Example.

SELECT `name`, `phone`, `product` FROM `tbmMember` WHERE `dateofServicw` < (Day,-30,GETDATE()); 

Solution 4 - Sql

Try adding this to your where clause:

dateadd(day, -30, getdate())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionInnovaView Question on Stackoverflow
Solution 1 - SqlamelvinView Answer on Stackoverflow
Solution 2 - SqlMerin NakarmiView Answer on Stackoverflow
Solution 3 - SqlAshley2605View Answer on Stackoverflow
Solution 4 - SqlChester Porcioncula VelascoView Answer on Stackoverflow