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

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)

Related

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.

Date filter on column in sql

I have query that filtered by date, for now it take only the last 24h from the moment I execute it, for doing that I'm using the next code:
( DateDiff(HH, vw_public_task.complete_date, getdate()) < 25)
There is a way that my date filter will give query results for the last 24h but not depending on my current hour but according to "day 08:00am" -- "day+1 08:00am" at any time that I execute it?.
For example if I execute my query now I want to see date results from yesterday 08:00am till today 08:00am.
You can calculate yesterday 8am using the formula:
-- Yesterday at 8 am.
SELECT
DATEADD(HOUR, 8, CAST(CAST(DATEADD(DAY, -1, GETDATE()) AS DATE) AS DATETIME)) AS Yesterday8AM
;
GetDate returns the current date. The innermost date add subtracts one day. Casting this as a date removes the timestamp. Casting this back to a DateTime gives yesterday at midnight. Now we are dealing with a DateTime we can use date add, again, to add 8 hours.
If you are using SQL Server 2012, or above, consider the native function DATETIME2FROMPARTS instead.
Use Date() (How to part DATE and TIME from DATETIME in MySQL).
( DateDiff(HH, vw_public_task.complete_date, Date(getdate())+8 ) < 25)

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

Get todays records from SQL database when stored in UTC time

I have user created records that I am storing the create date in UTC in a sql database.
I want to allow the user to select a date on the UI that will pull the records for that day.
My local time is -4:00 from the UTC time. So here is my issue:
If a user created a record at 9:00AM local time (1:00PM UTC), and then another record at 10:00PM local time (2:00AM UTC the next day), how do I query both those records out when they select that day?
The records are stored in UTC so they fall in 2 separate days, but they are actually in the same day for the local time.
How would I solve this? Should I even be storing the records in UTC?
Thanks
Is the -4 hours difference always there, or do you have multiple users from different time zones? If first one applies, consider dateadd. For example:
SELECT dateadd(hour, -4, yourTimeStamp) as TimeStamp
, CAST(dateadd(hour, -4, yourTimeStamp) as DATE) as DateOfTimeStamp
FROM YourTable
WHERE CAST(dateadd(hour, -4, yourTimeStamp) as DATE) = '20160510'
EDIT:
You can use:
SELECT DATEDIFF(MINUTE, SYSDATETIME(), GETDATE()) AS MinutesOffsetUTCtoLocal
to get the time difference in minutes between the client and the machine. Integrating this would look like this:
SELECT dateadd(minute, DATEDIFF(MINUTE, SYSDATETIME(), GETDATE()), yourTimeStamp) as TimeStamp
, CAST(dateadd(minute, DATEDIFF(MINUTE, SYSDATETIME(), GETDATE()), yourTimeStamp) as DATE) as DateOfTimeStamp
FROM YourTable
WHERE CAST(dateadd(minute, DATEDIFF(MINUTE, SYSDATETIME(), GETDATE()), yourTimeStamp) as DATE) = '20160510'
I suggest to build a calendar table with the following columns (will be handy for other things too) - in my examples it is called: local_calendar
local_date DATE -- This marks the day (without time)
date_starts_utc DATETIME -- Local midnight converted to UTC
date_ends_utc DATETIME -- Local midnight of the next day converted to UTC.
Fill this table from and to a reasonable date range.
From now, you can join this table to any UTC time to get the local date and any local dates to utc time boundaries.
To get the local date for a utc time
SELECT local_date
FROM local_calendar
WHERE #utcTime >= date_starts_utc AND #utcTime < date_ends_utc
To get the UTC boundaries for a local date
SELECT #lowerBoundary = date_starts_utc, #higherBoundary = date_ends_utc
FROM local_calendar
WHERE date = #localDate

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.

Resources