Fetching record of last 48 hrs from SQL - sql

I have table1 with with the columns DATETIME, ID and Status.
I will call my stored procedure after specific hours and I want to fetch records of last 48 non weekend hours.
I tried to do it by writing case for each day of getdate().
I want to know what is the best way to do this.

To avoid weekends, you can use a case or other logic in the where. Your logic isn't 100% clear on what to do on weekend days. But here is an approach:
where (datename(wk, getdate()) in ('Sunday', 'Monday', 'Tuesday') and datetime >= dateadd(hour, 48 + 48, getdate()) ) or
(datename, wk, getdate()) in ('Wednesday', 'Thursday', 'Friday') and datetime >= dateadd(hour, 48, getdate()) or
(datename , wk, getdate()) in ('Saturday') and datetime >= dateadd(hour, 24 + 48, getdate());

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

SQL statement to select all rows from previous day with time

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

How to create a time of 9pm today in SQL

I am trying to get a smalldatetime value of "9pm today" in a query. I thought I could use
DATEADD(HOUR, 21, CONVERT(date, GETDATE()))
but SQL Server doesn't like that - I get the error
The datepart hour is not supported by date function dateadd for data
type date.
Suggestions for a workaround?
Pretty simple, just cast date back to datetime after casting to date.
Thus you'll get current_date 00:00:00 and then add 21 hours:
select dateadd(hh, 21, cast(cast(getdate() as date) as datetime))
it is because dateadd's 3rd parameter should be datetime type, not date.
SELECT DATEADD(HOUR, 21, CONVERT(datetime,CONVERT(date, GETDATE())))
just add 21 / 24.0 to todays date
Select dateadd(day, datediff(day, 1, getDate()), 1) + (21 / 24.0)
First part, dateadd(day, datediff(day, 1, getDate()), 1), strips time from getdate(),
second part, + (21 / 24.0), adds fractional part of day equal to 9 am
This works because internally, SQL Server represents datetimes as two integers, one for the date, (number of days since 1 Jan 1900), and a second integer for the time, (number of ticks since midnight), which it combines into a decimal value where the integer part is the date integer, and the decimal part is the fraction of one day, so if you add 0.5 to a date, you get noon on that day, etc.
or, for comparison, using dateadd for hours,
Select dateadd(hour, 21, dateadd(day, datediff(day, 1, getDate()), 1))

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

How to get the last week date from Sunday to Saturday

I have the following table:
Date Value
4/13/2014 25
4/14/2014 35
4/15/2014 30
4/16/2014 25
4/17/2014 21
4/18/2014 20
4/19/2014 42
4/20/2014 54
4/21/2014 44
4/22/2014 47
4/23/2014 48
4/24/2014 34
4/25/2014 32
4/26/2014 18
4/27/2014 20
4/28/2014 32
4/29/2014 34
Which updates daily.
How can I have a SQL query which adds the total Value from last week (Sunday-Saturday)?
In this case, the Sunday is 4/20 and Saturday is 4/26.
I currently have the following query which gets the week from this week:
SELECT * , DATENAME(WEEKDAY, Date) AS [DAY], CAST(CONVERT(VARCHAR(12), DATEADD(DAY , 7-DATEPART(WEEKDAY,GETDATE()),GETDATE()), 110) AS DATE) AS 'WeekEnding'
FROM [database].[dbo].[table]
WHERE Date >= cast(dateadd(day,1-datepart(dw, getdate()), getdate()) as date)
--SUNDAY to SATURDAY
If I understand your question correctly, you'd like to get the SUM of the Value field within the timespan of the last Sunday-Saturday. Since you said that the table is updated daily, I'll assume that you mean the last actual week, and not just the last week that happens to be in the DB.
Here's one query that sums the records by filtering between Sunday-Saturday. In your example data, the total is 277.
SELECT SUM(Value) AS TotalValue
FROM ValueTable
WHERE Date >= DATEADD(day, -((DATEPART(dw, GETDATE()) + ##DATEFIRST) % 7) - 6,
DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)) AND
Date <= DATEADD(day, -(DATEPART(dw, GETDATE()) + ##DATEFIRST) % 7,
DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))
Explanation: the DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) parts simply removes the time portion of the current datetime. The other part, DATEADD(day, -(DATEPART(dw, GETDATE()) + ##DATEFIRST) % 7) subtract the difference between the current date's weekday and Saturday, yield a date of last Saturday.
For Oracle you could try. IW - week number in a year, according to ISO standard:
SELECT to_char( Date , 'IW' ), SUM (Value)
FROM TableA
GROUP BY to_char( Date , 'IW' );
The simpler way to get data from last Saturday to Sunday is:
select * from my_table_name where my_date between
DATEADD(dd, DATEPART(DW,GETDATE())*-1+1, GETDATE()) AND DATEADD(dd, DATEPART(DW,GETDATE())*-1+7, GETDATE())
Where DATEADD(dd, DATEPART(DW,GETDATE())*-1+1, GETDATE()) is last Saturday and
DATEPART(DW,GETDATE())*-1+7, GETDATE()) is the coming Sunday.