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
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.
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.
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.
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)
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With