Query to get content older than 3 weeks
How about:
select contentID, title, created
from content
where created < dateadd(week,-3,getdate());
This sticks closer to the question. 21 days is fine, obviously means the same, but I find that it's good to use the terminology used in the question.
For example... a while back I was asked to survey an average of 1 in 50 visitors to a site. I described this as a proportion of 0.02, and the client wasn't happy. I pointed out to the client that they're the same, but I learned my lesson, and now if I change the way that something is described, I make sure I comment to that effect, and preferably don't change it in the first place. If the client wants 3 weeks, do it as 3 weeks, not 21 days.
SQL Server: How to get rows where the date is older than X years?
SELECT * FROM dbo.Assets
WHERE DATEDIFF(YEAR, AcquiredDate, GetDate()) >= 8
For an performance optimized query look at @Horaciuxs answer.
How do I query for all dates greater than a certain date in SQL Server?
select *
from dbo.March2010 A
where A.Date >= Convert(datetime, '2010-04-01' )
In your query, 2010-4-01
is treated as a mathematical expression, so in essence it read
select *
from dbo.March2010 A
where A.Date >= 2005;
(2010 minus 4 minus 1 is 2005
Converting it to a proper datetime
, and using single quotes will fix this issue.)
Technically, the parser might allow you to get away with
select *
from dbo.March2010 A
where A.Date >= '2010-04-01'
it will do the conversion for you, but in my opinion it is less readable than explicitly converting to a DateTime
for the maintenance programmer that will come after you.
Date filtering from past two weeks
You can compare the created_on
column with an expression that calculates "two weeks before today":
un.created_on >= current_date - interval '2 weeks'
alternatively you can also subtract 14 days
un.created_on >= current_date - 14
Related Topics
Replacing Text in a Blob Column
Date Split-Up Based on Fiscal Year
How to Insert Data Directly from Excel to Oracle Database
Failed to Create an Assembly in Sql
Sql Server Bug or Feature? Decimal Numbers Conversion
How to Select Row with Max Value When Duplicate Rows Exist in SQL Server
Sql Dynamic Order by Using Alias
Query to Check Overlapping Ranges in SQL Server
How to Use Oracle Outer Join with a Filter Where Clause
Sql - Create Database and Tables in One Script
Wordpress: Automatically Delete Posts That Are X Days Old
Merge Two Tables/Concatenate Values into Single Column
Using Nvl for Multiple Columns - Oracle Sql
Sql Access Query- Update Row If Exists, Insert If Does Not
Sql - Query to Insert a Column Value If It Does Not Exist in That Column
Comparing Comma Separated Values from Two Columns of Two Different Tables