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
Check If Null Exists in Postgres Array
Generate Default Values in a Cte Upsert Using Postgresql 9.3
Extbase - Get Created SQL from Query
SQL Server for Xml Path Make Repeating Nodes
How to Transform Comma Separated Column into Multiples Rows in Db2
Function-Based Indexes in SQL Server
How to Create a Decimal Field in Access with Alter Table
Oracle- Split String Comma Delimited (String Contains Spaces and Consecutive Commas)
How to Use If/Else Statement to Update or Create New Xml Node Entry in SQL
Checking If a String Is Found in One of Multiple Columns in MySQL
Hamming Distance/Similarity Searches in a Database
Select a Column If Other Column Is Null