Get date Minus 1 day but at exacty 4 AM - sql-server

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.

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' ) )

Get todays date a year ago

I have a script where I need today date but from a year ago, however I also need it to be in the following format with the time.
2017-11-07 00:00:00.000
I currently have got to a place where I have last years date just not the 00:00:00.000 timestamp.
select DATEADD(year, -1, GETDATE())
This returns the time of the time the query was ran.
2017-11-07 13:37:10.770
This is for a where clause as I need to get some data from today's date from last year so looking for a where clause that starts at this date last year at midnight and finishes at 23:00:00.000
It's for a client running SQL Server 2005.
Try using:
CONVERT(DateTime, DATEDIFF(DAY, 0, DATEADD(year, -1, GETDATE())))
Here is a query to see this in action:
SELECT
GETDATE() AS today,
DATEADD(year, -1, GETDATE()) AS today_last_year, -- what you already have
CONVERT(DateTime, DATEDIFF(DAY, 0, DATEADD(year, -1, GETDATE()))) AS
today_last_year_midnight;
This returned (as of the time of writing this answer):

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 data between two times for the last 30 days

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

How to retrieve all rows with a date in this and the next week? SQL Server

I have a Tasks table that contains Date, Description and WorkerID columns. I want to write a stored procedure that selects all tasks from this week and the following week. Can someone help me get to a solution please? I am not so good at this.
If indexing (performance) isn't a concern then it's a simple expression if your week coincides with the SQL Server setup:
where datediff(week, getdate(), [Date]) between 0 and 1
The snippet below is another option that works with both dates and datetimes. It's possible to adapt it to work with any ##datefirst setting but I'm just going to assume that the setting matches with the start of week you're looking to report on.
where
[Date] >= dateadd(day, 1 - datepart(weekday, getdate()), cast(getdate() as date))
and [Date] < dateadd(day, 15 - datepart(weekday, getdate()), cast(getdate() as date))
The following should give you what you want (assuming Monday is the first day of the week, and that the DBMS is SQL Server):-
select *
from Tasks
where [Date]
between
dateadd(dd,-(datepart(dw,[Date])-2),[Date]) --Monday of this week
and
dateadd(dd,13,dateadd(dd,-(datepart(dw,[Date])-2),[Date])) --Sunday of next week

Resources