How to Subtract 30 Days from the Current Date Using SQL Server

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())

Subtract 30 days from current date in where clause

ADS allows you to do straight math on dates (if you've got an actual date column in the table), so you can use

WHERE datecol between curdate() - 30 and curdate()

How to subtract 30 days from the current datetime in mysql?

SELECT * FROM table
WHERE exec_datetime BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW();

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

SQL Query Where Date = Today Minus 7 Days

declare @lastweek datetime
declare @now datetime
set @now = getdate()
set @lastweek = dateadd(day,-7,@now)

SELECT URLX, COUNT(URLx) AS Count
FROM ExternalHits
WHERE datex BETWEEN @lastweek AND @now
GROUP BY URLx
ORDER BY Count DESC;

Subtract one day from datetime

Try this

SELECT DATEDIFF(DAY,  DATEADD(day, -1, '2013-03-13 00:00:00.000'), GETDATE())

OR

SELECT DATEDIFF(DAY,  DATEADD(day, -1, @CreatedDate), GETDATE())

Subtracting dates in Proc SQL/SAS

Using the PUT() function to convert the current date into a character string is going to make it impossible to perform arithmetic with the result.

If COL_C has a DATE value it does not matter how the value is displayed (formats just impact how values are displayed). A DATE value is just the number of days since 1960. You only need to subtract the two numbers to calculate the difference in days.

 (date() - col_C) as age_bucket

If COL_C has a DATETIME value (number of seconds since 1960) then first convert it to a DATE value.

 (date() - datepart(col_C)) as age_bucket

If COL_C is a character string in the style YYYY-MM-DD then use the INPUT() function with the YYMMDD informat to convert the string into a date value.

 (date() - input(col_C,yymmdd10.)) as age_bucket


Related Topics



Leave a reply



Submit