SQL statement to select all rows from previous day with time - sql

Need to select rows from the previous day but before 08:00 am.
(date >= dateadd(day,datediff(day,1,GETDATE()),0)
AND CONVERT(varchar, date,108) BETWEEN '00:00:00' AND '08:00:00')
return rows of from the previous day and before 08:00 am.

You can use:
where date >= dateadd(day, -1, convert(date, getdate())) and
date < dateadd(hour, -16, convert(date, getdate()))
This query is structured so it can make use of indexes.
You can also phrase this as:
where convert(date, [date]) = dateadd(day, -1, convert(date, getdate()) and
convert(time, [date]) <= '08:00:00'
This should also use indexes, because conversion to a date is perhaps the only function that does not prevent the use of an index.

You're on the right track. This does a BETWEEN check on the beginning of the day yesterday and the beginning of the day yesterday + 8 hours:
date BETWEEN dateadd(day,datediff(day,1,GETDATE()),0)
AND dateadd(hour, 8, dateadd(day,datediff(day,1,GETDATE()),0))

Related

SQL, get any data between two days and specific time

I am trying to get any data that is between that time range of two days ago until yesterday.
Example: Retrieve any data between 3 PM two days ago and yesterday 3 PM. This query should work on the daily basis.
I am thinking something like but just don't know where to insert the time
select * from dbo.table where system_date between getdate()-2 and getdate()-1
You can use CAST(CAST(GETDATE() AS date) AS datetime) to get the beginning of today's date, then use DATEADD to subtract 1 or 2 days, and add 15 hours.
I strongly suggest you use >= AND < on dates, rather than BETWEEN, otherwise you get "on the interval" issues.
SELECT t.*
FROM dbo.[table] t
WHERE t.system_date >= DATEADD(hour, 15, DATEADD(day, -2, CAST(CAST(GETDATE() AS date) AS datetime)))
AND t.system_date < DATEADD(hour, 15, DATEADD(day, -1, CAST(CAST(GETDATE() AS date) AS datetime)));
try this
select *
from dbo.table
where system_date between dateadd(day, datediff(day, 2, getdate()), '15:00:00') and dateadd(day, datediff(day, 1, getdate()), '15:00:00')
You should use DATEADD for subtracting dates. Your query will look like this.
select *
from table
where system_date between dateadd(day, -2, getdate()) and dateadd(day, -1, getdate())

Categorize data by time periods

I am looking for a SQL statement to select all rows from the table with time periods like Today, Yesterday..etc. The table holds one datetime column.
Here result set should automatically populate results with pretty date (Today, yesterday,.. Last month, Older)and number of records on the periods,
Today -- 10
Yesterday -- 35
If you are looking to define the time periods, then use case. You don't really define the exact definitions, but something like this:
select (case when col >= convert(date, getdate()) then 'Today'
when col >= dateadd(day, -1, convert(date, getdate())) then 'Yesterday'
when col >= dateadd(day, -6, convert(date, getdate())) then datename(weekday, col)
when col >= dateadd(day, -13, convert(date, getdate())) then 'Last Week'
when col >= dateadd(day, -20, convert(date, getdate())) then 'Two Weeks Ago'
when col >= dateadd(day, -27, convert(date, getdate())) then 'Thre Weeks Ago'
else 'Older'
end)
Here is a db<>fiddle.

Getdate() functionality returns partial day in select query

I have a query -
SELECT * FROM TABLE WHERE Date >= DATEADD (day, -7, -getdate()) AND Date <= getdate();
This would return all records for each day except day 7. If I ran this query on a Sunday at 17:00 it would only produce results going back to Monday 17:00. How could I include results from Monday 08:00.
Try it like this:
SELECT *
FROM SomeWhere
WHERE [Date] > DATEADD(HOUR,8,DATEADD(DAY, -7, CAST(CAST(GETDATE() AS DATE) AS DATETIME))) --7 days back, 8 o'clock
AND [Date] <= GETDATE(); --now
That's because you are comparing date+time, not only date.
If you want to include all days, you can trunc the time-portion from getdate(): you can accomplish that with a conversion to date:
SELECT * FROM TABLE
WHERE Date >= DATEADD (day, -7, -convert(date, getdate())
AND Date <= convert(date, getdate());
If you want to start from 8 in the morning, the best is to add again 8 hours to getdate.
declare #t datetime = dateadd(HH, 8, convert(datetime, convert(date, getdate())))
SELECT * FROM TABLE
WHERE Date >= DATEADD (day, -7, -#t) AND Date <= #t;
NOTE: with the conversion convert(date, getdate()) you get a datatype date and you cannot add hours directly to it; you must re-convert it to datetime.
Sounds like you want to remove the time. Correct? If so then do the following.
SELECT * FROM TABLE WHERE Date >= (DATEADD (day, -7, -getdate()) AND Date DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0))

Query for all records from 1:01 AM two days ago to 1 AM yesterday

I'm currently trying to write a query as part of an SSIS package, and part of the requirements is that the query will return all rows with a time stamp (currently in yyyy-mm-dd hh:mm:ss:mmm format) occuring from 1:01 AM 2 days ago to 1 AM 1 day ago. I can write this for dates, but I'm stuck on how to incorporate the hour range. Current query:
SELECT *
FROM
(SELECT dateadd(S, logintime, '1970-01-01') as "conversion"
,DATEPART(hh, dateadd(HOUR, logintime, '1970-01-01')) as "HourCol"
,DATEPART(hh, dateadd(MINUTE, logintime, '1970-01-01')) as "MinuteCol"
, * FROM my.logtable
WHERE row_date between dateadd(day, -3, getdate()) and DATEADD(day, -1, GETDATE())) as subselect
where conversion between --and from here I'm lost.
order by conversion
SELECT getdate() today,
CAST(getdate() as DATE) today_0000,
DATEADD(day, -1, CAST(getdate() as DATE)) yesterday0000,
DATEADD(day, -2, CAST(getdate() as DATE)) daybeforyesterday0000,
DATEADD(mi, 60, CAST(DATEADD(day, -1, CAST(getdate() as DATE)) as datetime)) yesterday0100,
DATEADD(mi, 61, CAST(DATEADD(day, -2, CAST(getdate() as DATE)) as datetime)) daybeforyesterday0101
Last two give you the range you are looking for. As long you compare with another datetime should work.

Select every date where date is equal to today's date minus 1 day - Not working with dateadd on Month/year - T SQL

Like the title says, I am trying to build a query that selects all records from a database table where the date is equal to yesterdays date.
The date column in the table is however of the format datetime (with hours, minutes, seconds as well) so I do the select based on the dates year, month and day (times don't matter as long as the date is yesterday).
To achieve this I have build the following query:
SELECT
*
FROM
qryTouchBoekingen
WHERE
(DATEPART(yyyy, myDateTime) = DATEADD(dd, -1, Datepart(dd, GetDate()))
AND (DATEPART(mm, myDateTime) = DATEADD(dd, -1, Datepart(mm, GetDate()))
AND (DATEPART(dd, myDateTime) = DATEADD(dd, -1, Datepart(dd, GetDate())) )
ORDER BY
Starttijd ASC
Though this doesn't return any records. When I only use it on the day part of the myDateTime column then it works (but obviously also returns all other years and months with that specific date).
I also couldn't do it using:
SELECT
*
FROM
qryTouchBoekingen
WHERE
myDateTime = DATEADD(dd, -1, GetDate())
because this give errors on the time.
How about this much simpler version:
cast(myDateTime as date) = cast(dateadd(day, -1, getdate()) as date)
Or, even better:
(myDateTime >= cast(dateadd(day, -1, getdate()) as date) and
myDateTime < cast(getdate() as date)
)
This version is more explicit in its ability to take advantage of an index. (The first will also take advantage of an index on myDateTime, but that is an exception to the rule that functions preclude the use of indexes.)
Try this one.
You should count previous day before making datepart from it
Select * from qryTouchBoekingen
WHERE ( DATEPART(yyyy, myDateTime) = Datepart(dd, DATEADD(dd, -1, GetDate()))
AND (DATEPART(mm, myDateTime) = Datepart(mm, DATEADD(dd, -1, GetDate()))
AND (DATEPART(dd, myDateTime) = Datepart(dd, DATEADD(dd, -1, GetDate())) )
Order by Starttijd ASC
DOes this work:
Select
*
from
qryTouchBoekingen
WHERE
CAST( myDateTime AS DATE) = CAST(DATEADD(day, -1, GetDate()) AS DATE)
Order by
Starttijd ASC