Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract 30 days from the current date using SQL Server

I am unable subtract 30 days from the current date and I am a newbie to SQL Server.

This is the data in my column

date  ------------------------------ Fri, 14 Nov 2014 23:03:35 GMT Mon, 03 Nov 2014 15:18:00 GMT Tue, 11 Nov 2014 01:24:47 GMT Thu, 06 Nov 2014 19:13:47 GMT Tue, 04 Nov 2014 12:37:06 GMT Fri, 1 Nov 2014 00:33:00 GMT Sat, 5 Nov 2014 01:06:00 GMT Sun, 16 Nov 2014 06:37:12 GMT 

For creating the above column I used varchar(50) and now my problem is I want to display the dates for past 15-20 days from the date column can any one help with this issue ? update [ how can i display last 7 days dates in order

like image 426
Madpop Avatar asked Dec 02 '14 11:12

Madpop


People also ask

How do I subtract dates in SQL Server?

If you would like to subtract dates or times in SQL Server, use the DATEADD() function. It takes three arguments. The first argument is the date/time unit – in our example, we specify the day unit. Next is the date or time unit value. In our example, this is -30, because we’re taking 30 days away from the current date.

How to subtract 30 days from current datetime in MySQL?

To subtract 30 days from current datetime, first we need to get the information about current date time, then use the now () method from MySQL. The now () gives the current date time.

How to subtract 30 days from the expiration date in SQL?

In SQL Server you can use the DATEADD () function to “subtract” 30 days from the Expiration Date. Here’s the query to use: select CouponID, CouponName, CouponDescription, PercentDiscount, ExpirationDate, dateadd (d,-30,ExpirationDate) StartDate from Coupon To subtract 30 days, we add negative 30 days; tricky.

How do you add 30 days to a date in SQL?

Add 30 days to a date SELECT DATEADD(DD,30,@Date) Add 3 hours to a date SELECT DATEADD(HOUR,-3,@Date) Subtract 90 minutes from date SELECT DATEADD(MINUTE,-90,@Date)


1 Answers

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()) 
like image 78
HoneyBadger Avatar answered Oct 04 '22 13:10

HoneyBadger