Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select last one week data from today's date

How to select week data (more precisely, last 7 days data) from the current date in the fastest way as I have millions or rows in the table. I have a time stamp of created_date in sql table.

I have tried this

SELECT Created_Date FROM Table_Name WHERE Created_Date >= DATEADD(day,-7, GETDATE()) 

I have two question:

  1. Is this query is correct?
  2. Is this is the fastest way to get the last seven day data from a table having millions of rows ?
like image 720
Zerotoinfinity Avatar asked Nov 07 '12 08:11

Zerotoinfinity


Video Answer


1 Answers

Yes, the syntax is accurate and it should be fine.

Here is the SQL Fiddle Demo I created for your particular case

create table sample2 (     id int primary key,     created_date date,     data varchar(10)   )  insert into sample2 values (1,'2012-01-01','testing'); 

And here is how to select the data

SELECT Created_Date FROM sample2 WHERE Created_Date >= DATEADD(day,-11117, GETDATE()) 
like image 160
Ahmad Avatar answered Sep 26 '22 01:09

Ahmad