Find Last Few Days Of Query

If you ever wanted a simple way to get the last several days history using a DateTime column you can simply create a couple of DateTime Variables, and supply them with todays date, or todays date minus several days. This way you can include the last 7 days in your query logic.

Here’s a quick example:

use MyDatabase
set nocount on
declare @today datetime = ( select getdate() )
declare @last_7_days datetime = ( select getdate() - 7 ) -- today minus 7 days
, 'CreateDate' = convert(char, CreateDate, 9)
, 'CreateDate' = CreateDate
CreateDate > @last_7_days -- include it with a greater than sign in the where clause and you will get the last several days. order by
timestamp asc

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s