Get data between two times for the last 30 days - sql-server

I have a query to pull data from the last 30 days and I am using this function
DATEADD(day, -30, GETDATE())
I am getting the data but it looks like it is missing some Like when I look at the data for specific date it doesn't include all the 24 hours data I am missing some. I need the data to include the 24 hours data for every day for the last 30 days.
select
*
from
Travel R
where
R.s9_date > CONVERT(date, DATEADD(day, -30, GETDATE()))
order by
R.s9_date
Thanks

GETDATE() returns a datetime which includes the hours, minutes, seconds, etc. Therefore when you subtract 30 days from that, the subtracted value will also have the time part.
Assuming you're using SQL Server 2008 or above, you can convert the datetime to a date by wrapping the DATEADD function with a CONVERT function:
CONVERT(date, DATEADD(day, -30, GETDATE()))

I believe CAST was introduced in SQL Server 2005
select
*
from
Travel R
where
R.s9_date > CAST(DATEADD(DAY, -30, GETDATE()) AS DATE)
order by
R.s9_date

Related

Is there a convenient way to compare week-over-week data in SQL?

I have a table full of daily aggregate data, but I occasionally need to pull weekly aggregate data, and provide info on increases or decreases. For that reason, I was considering using T-SQL DATEPART functionality to get week-number and year info for dates.
For example, I can get the following info using today's date (9/11/2020):
#nowWeekNumber int = datepart(wk,#today), --yields 37
#nowYear int = datepart(year,#today), --yields 2020
Using that logic, I could then gather info on records where year is 2020 and weekNumber is 36, and then I could compare those numbers to get a weekly increase/decrease. (Or maybe I'd compare weeks 35 and 36 to ensure that I'm dealing w/ entire weeks, but you get the picture)
However, if the date is 2021-01-03, that's going to return a year of 2021, and a weekNumber of 2. If I subtract a week, I'm going to get year 2021 and weekNumber 1. That weekNumber is only going to contain January 1st and 2nd, because 12/27 thru 12/31 are considered year 2020 and weekNumber 53 (even though the calendar week is 12/27 thru 1/2).
In other words, I don't think I can use weekNumber to gather weekly data, even though that would be fairly convenient. I'm aware that I can use DATEADD functions to grab the start and end-date for consecutive weeks, and I can then gather aggregate data for records BETWEEN those dates, but is there a more-convenient way to do this?
Why don't you consider using dateDiff as key function? As...
select dateDiff(wk, 0, getDate())
Returns a single integer for the whole week (6297 for '20200911') and :
select dateAdd(wk, dateDiff(wk, 0, getDate()), 0),
dateAdd(dd, 6, dateAdd(wk, dateDiff(wk, 0, getDate()), 0))
or
select dateAdd(wk, 6297, 0),
dateAdd(dd, 6, dateAdd(wk, 6297, 0))
gives you the 1st and last day of that week.
You can use DATEPART but instead of wk you can use the iso week. Then you don't have the problem with a week being split in 2. To be sure also use SET DATEFIRST to define exactly on which day the week starts.
SET DATEFIRST 1; --use monday as first day of the week
SELECT datepart(iso_week,'2021-01-01');
SELECT datepart(iso_week,'2021-01-03');
SELECT datepart(iso_week,'2021-01-04');
The other option is to create your own calendar table and join that to your daily table.
EDIT: for a week start on sunday
SET DATEFIRST 7;
SELECT DATEPART(WEEK, DATEADD( DAY, 1-DATEPART(WEEKDAY,'2020-12-27'),'2020-12-27' ) )
SELECT DATEPART(WEEK, DATEADD( DAY, 1-DATEPART(WEEKDAY,'2020-12-28'),'2020-12-28' ) )
SELECT DATEPART(WEEK, DATEADD( DAY, 1-DATEPART(WEEKDAY,'2021-01-01'),'2021-01-01' ) )
SELECT DATEPART(WEEK, DATEADD( DAY, 1-DATEPART(WEEKDAY,'2021-01-02'),'2021-01-02' ) )

How do I pull data from 30 months ago and prior?

I am trying to automate my code so that it can only pull data from 30 months ago, since that is around when a data set is fully complete for the year. So for example, when it is July 1st, 2019, the code should pull from January 1st 2017 which is 30 months prior. I thought that the code I wrote would do this, but there are still instances of 2018 records showing up. Am I properly using this dateadd function?
select *
from table
where month(period_end) <= month(dateadd(month, -30, getdate()))
This runs fine, but 2018 records are still showing up when I should only see 2017 and before.
The issue is with the use of the MONTH() function -- that returns a number, 1-12, so you're actually comparing the month number of period_end against the month number 30 months ago. Replace your WHERE with
WHERE period_end <= DATEADD(MONTH, -30, GETDATE())
Simplify it, you don't need month():
where period_end <= dateadd(month, -30, getdate())
dateadd(month, -30, getdate()) returns the maximum date you want period_end to compare to.

DATEADD(day, -7, GETDATE()) - Does it take time into account?

I am writing a SQL query in Aginity through Amazon Redshift to extract the last 7 days of data. The Date column which I am calling is in the variable type:
DATE
An example output is this:
5/30/2017 0:00
When I call the below function, does it matter what time of day I run this query or will it always take the full day's worth of data?
WHERE Date >= DATEADD(day,-7, GETDATE())
Yes, this includes the current time component when subtracting 7 days. To get rid of that, convert to a date:
WHERE Date >= CAST(DATEADD(day, -7, GETDATE()) as DATE)

Get date Minus 1 day but at exacty 4 AM

I have the following sql server WHERE clause:
WHERE (DateCreated >= CONVERT(datetime, GETDATE(), 111) - 1)
This gets the date (where today is 2015-06-09) 2015-06-08. I need to add a time to this as well like 2015-06-08 04:00:00 in 24H format. the time will always be the same bat every time the SQL command is executed, it should only be from yesterday at 4 AM to the current date and time.
how can this be achieved?
Try this:
WHERE DateCreated >= dateadd(d, datediff(d, 1, getdate()), '04:00')
I think you are looking for:
WHERE (DateCreated >= DATEADD(HOUR, 4,
CONVERT(datetime,
DATEADD(DAY, -1, CONVERT(date, GETDATE()) )
)
)
)
Converting directly to DATE will take away the hassle of taking care of the hour part. After that, doing a DATEADD with -1 will take you 1 day ago.
After this step, simply convert it back to datetime to create a timestamp part to your date, which is defaulted to 00:00:00.000.
And in the end, simply add 4 hours to this start date, which will always give you 4:00 AM.

Select Records for which the date field is spent a year

I have a table with columns
TypeExame, DateExame
How can I select all records with (Now-DateExame) > = 365 days?
select *
from MyTable
where datediff (day, DateExame, getdate()) >= 365
See DATEDIFF and GETDATE for further details.
If you have an index on DateExame, putting the condition like this will enable its usage:
SELECT *
FROM atable
WHERE DateExame <= DATEADD(DAY, -365, CAST(GETDATE() AS date))
;
The above references the date data type, which means you'd need at least SQL Server 2008 to run that. To make it work in earlier versions, you could rewrite the condition like this:
WHERE DateExame <= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) - 365, 0)
Basically, the modified version uses the DATEADD/DATEDIFF method of truncating a datetime value with a minor tweak to also subtract 365 days along the way.
However, if all your DateExame values are dates without the time part, this simpler version should work just as well:
WHERE DateExame <= DATEADD(DAY, -365, GETDATE())
That is, removal of GETDATE()'s result's time part would be perfectly unnecessary.
Try this one :
select *
from MyTable
where datediff (day, DateExame, getdate()) >= 365
By Using Datediff function you can subtract two dates . you can pass first argument as minute , day , month , year etc .

Resources