I want to select all the records from [Orders] that have a [Submissiondate] less than 7 days.
I'm completely stumped. This is the query I'm executing:
SELECT * FROM [Orders] WHERE ([SubmissionDate] < @SubmissionDate)
Doesn't work.
I want to select all the records from [Orders] that have a [Submissiondate] less than 7 days.
I'm completely stumped. This is the query I'm executing:
SELECT * FROM [Orders] WHERE ([SubmissionDate] < @SubmissionDate)
Doesn't work.
If you mean you want rows with SubmissionDate between @SubmissionDate and @SubmissionDate - 7 days, then this is how I would implement that in Transact-SQL:
WHERE [SubmissionDate] BETWEEN DATEADD(DAY, -7, @SubmissionDate)
AND @SubmissionDate
Note that BETWEEN implies >= and <=. If you need strict inequalities, make it something like this:
WHERE [SubmissionDate] > DATEADD(DAY, -7, @SubmissionDate)
AND [SubmissionDate] < @SubmissionDate
Assuming that the sql parameter @SubmissionDate is the date (and time) now. You could use the following query that will return those [Orders] submitted within the last 7 days:
SELECT * FROM [Orders] WHERE ([SubmissionDate] >= DATEADD(DD, -7, DATEADD(dd, 0, DATEDIFF(dd, 0, @SubmissionDate))))
Two important remarks to this solution:
[Orders] submitted
'today' (until the time the query is being executed).The following code is just to get the date 'part' only of a date-time (extracted from this other SO thread).
DATEADD(dd, 0, DATEDIFF(dd, 0, @SubmissionDate))
Try this
SELECT * FROM [Orders] WHERE [submission_date] < NOW() - INTERVAL 7 DAY;