SQL WHERE clause to pick data for last 7 days
use where clause like below
select t.* from your_table t
where CONVERT(datetime, creation_date,11)>= DATEADD(day,-7, GETDATE())
How to select last seven day's dates in SQL Server
If your expected results listed in the question are exactly what you are looking for, this will do the trick:
SELECT * FROM (
SELECT CAST(GETDATE() AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -1, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -2, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -3, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -4, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -5, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -6, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -7, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
) AS the_table
results:
Last_Seven_Days_Dates
---------------------
2018-07-05
2018-07-06
2018-07-07
2018-07-08
2018-07-09
2018-07-10
2018-07-11
2018-07-12
Since you stated you'll want these in a temp table, I've updated the answer to make it a subquery, a little easier to join or do something with.
Additionally if you need it in a temporary table you could do this:
SELECT * INTO #TempTableName FROM (
SELECT CAST(GETDATE() AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -1, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -2, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -3, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -4, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -5, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -6, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
UNION
SELECT CAST(DATEADD(DAY, -7, GETDATE()) AS DATE) AS Last_Seven_Days_Dates
)
How can I select the data from the last 7 days between hours
I can use between getdate() and getdate()-7 to get the period but I have no idea how to filter the time.
You can just use DATEPART(HOUR, datetimevalue)
, to get the hour.
For example, if you want only times from 11pm to 7pm:
SELECT *
FROM theTable
WHERE (DATEPART(HOUR, CREATED) > 22 --greater than 10pm hour
OR --OR
DATEPART(HOUR, CREATED) < 8 --less than 8pm hour
)
--put any other datediff logic here
Produces output:
COD CREATED
6002 2017-11-22 23:03:24.3594 -02:00
6003 2017-11-23 06:03:24.7344 -02:00
6005 2017-11-23 23:56:00.6874 -02:00
6006 2017-11-24 05:33:58.4243 -02:00
6008 2017-11-24 23:42:50.9885 -02:00
In this case I used datetimeoffset(4) for the CREATED column to allow for the -02:00 timezone.
If you want to see the full code: http://sqlfiddle.com/#!6/963a1/7/0
SQL Get last 7 days from event date
If you have values for all dates, without gaps, then you can use window functions with a rows
frame:
select
date,
count(*) cnt
sum(count(*)) over(order by date rows between 7 preceding and current row) cnt_d7
from mytable
group by date
order by date
Select Last N days data based on datetime column
In MariaDB 10.2.32 you can use DENSE_RANK()
window function:
SELECT *
FROM (
SELECT *, DENSE_RANK() OVER (ORDER BY DATE(timer) DESC) rnk
FROM tablename
) t
WHERE rnk <= 3
See a simplified demo.
Related Topics
How to Compare Timestamp Dates With Date-Only Parameter in MySQL
Make SQL Select Same Row Multiple Times
Er_Access_Denied_Error: Access Denied for User ''@'Localhost' (Using Password: No)
Constraint for Phone Number in SQL Server
Two Rows With the Same Id and Two Different Values, Getting the Second Value into Another Column
How to Divide Sum in Number from Count in Having
How to Change Date Format in .Net Rdlc Report
How to Escape Back Slash in SQL Server
How to Resolve MySQL Error Code: 1292. Truncated Incorrect Double Value
How to Extract Year from Hire_Date in My SQL
How to Replace Empty String With Value That Is Not Empty for the Same Policynumber
How to Subtract One Month from a Date Column
How to Convert from Blob to Text in MySQL
Using Node.Js (Express) and MySQL to Insert for a Timestamp
Left Join Without Duplicate Rows from Left Table
Mysql Select Rows on First Occurrence of Each Unique Value
Concatenate With String But Exclude When Null
Ssis Failed Validation and Returned Validation Status "Vs_Isbroken"