How to subtract 30 days from the current date using SQL Server
You can convert it to datetime
, and then use DATEADD(DAY, -30, date)
.
See here.
edit
I suspect many people are finding this question because they want to substract from current date (as is the title of the question, but not what OP intended). The comment of munyul below answers that question more specifically. Since comments are considered ethereal (may be deleted at any given point), I'll repeat it here:
DATEADD(DAY, -30, GETDATE())
Try this:
SELECT GETDATE(), 'Today'UNION ALLSELECT DATEADD(DAY, 10, GETDATE()), '10 Days Later'UNION ALLSELECT DATEADD(DAY, –10, GETDATE()), '10 Days Earlier'UNION ALLSELECT DATEADD(MONTH, 1, GETDATE()), 'Next Month'UNION ALLSELECT DATEADD(MONTH, –1, GETDATE()), 'Previous Month'UNION ALLSELECT DATEADD(YEAR, 1, GETDATE()), 'Next Year'UNION ALLSELECT DATEADD(YEAR, –1, GETDATE()), 'Previous Year'
Result Set:
———————– —————2011-05-20 21:11:42.390 Today2011-05-30 21:11:42.390 10 Days Later2011-05-10 21:11:42.390 10 Days Earlier2011-06-20 21:11:42.390 Next Month2011-04-20 21:11:42.390 Previous Month2012-05-20 21:11:42.390 Next Year2010-05-20 21:11:42.390 Previous Year
TRY THIS:
Cast your VARCHAR value to DATETIME and add -30 for subtraction. Also, In sql-server the format Fri, 14 Nov 2014 23:03:35 GMT was not converted to DATETIME. Try substring for it:
SELECT DATEADD(dd, -30, CAST(SUBSTRING ('Fri, 14 Nov 2014 23:03:35 GMT', 6, 21) AS DATETIME))