How can I filter data between 2 dates in SQL - sql

I have a report which i want to display the data within 3 days, i.e. when a user runs the report it should display data as of yesterday, today and tomorrow.
Where [tasSched].[CurrReqstDate] Between *yesterday today and tomorrow*

If you are using SQL Server you can use below query to get the data between yesterday and tomorrow.
WHERE [tasSched].[CurrReqstDate] between DATEADD(DAY, -1,GETDATE())
and DATEADD(DAY, +1,GETDATE())

Related

SQL Query Last Three Full Months (Excluding Current Month)

I have a database with millions of rows and many years of data. I'm currently using PowerBI to query the data (ODBC connection), but I don't want to pull everything. (Recent convert from Crystal.) Instead, I'm trying to write a clause into my WHERE statement to only query the last three full months of data (excluding the current month.) So, today being Jan 25,2023 I want to capture Oct 1 2022 - Dec 31 2022. I would then schedule this so it always pulls the last three months.
My dataset does contain a field that stores the date so I'm trying to write something like the below that I'm able to use to pull previous days.
"Table"."OrderDate" >= DATEADD(d,DATEDIFF(d,0,CURRENT_TIMESTAMP)-1,0) AND
"Table"."OrderDate" < DATEADD(d,DATEDIFF(d,0,CURRENT_TIMESTAMP),0))
Everything so far that I've tried (and found online) gives odd results. Pulls Nov - Jan and the like.
Thank you for any help you can provide.
You can use the following query to pull the last three full months of data. This query uses the DATEADD and DATEDIFF functions to calculate the date range for the last three full months.
WHERE "Table"."OrderDate" >= DATEADD(month, DATEDIFF(month, 0, CURRENT_TIMESTAMP) - 3, 0) AND "Table"."OrderDate" < DATEADD(month, DATEDIFF(month, 0, CURRENT_TIMESTAMP), 0)

SQL Filtering a date grabbing column and having it grab 10 days previous of record

I am trying to figure out how to query a database and go back 10 days from a certain date column. I am using SQL Server 2012.
Where Date_Complete IS (10 days ago or newer)
Any help with this would be greatly appreciated!
Did you mean this
WHERE Date_Complete >= DATEADD(day,-10, yourdatecolumn)
If you want to consider the time from today then it'd be
WHERE Date_Complete >= DATEADD(day,-10, GETDATE())

Searching for variable between two times in SQL

I have a variable date_entered which is in DateTime format. I made a temp field TIME which is just the time portion of date_entered. Im trying to search for the amount of tickets entered for each hour between 7am and 7pm. I need to get tickets for each hour increment between 7am and 7pm so tickets between 7-8,8-9,9-10 etc.. and they are to be displayed in different columns.
Right now I have:
=Iif((Hour(Fields!Time.Value) >= 7) AND
(Hour(Fields!Time.Value) < 8), Fields!TicketNbr.Value, 0)
However this is not getting only tickets between this hour interval and instead all tickets for that day. How can I get tickets just in that hour period? I am also using BIDS through Microsoft Visual Studio. Thanks!
Why do you need a temp field TIME? There's a TIME datatype on SQL Server 2008 which allows you to do something like this:
SELECT * FROM YourTable WHERE CAST(YourDateField AS TIME) BETWEEN '07:00' and '19:00'

SQL Date Issue for Weekly Report Data

I need to run a weekly report based on the arrival date. How can I set the arrival date in the where clause so that I can get the result only for each week. The hard part is I DO NOT want to modify the dates each week. I need the permanent where clause for the date. I have to provide a list of customers who arrived every week and I just want to run the same script without changing the week dates.
Please assist.
Thanks.
SELECT * FROM TABLE WHERE
(ARRIVAL_DATE>DATEFROMPARTS(YEAR(GETDATE()-7), MONTH(GETDATE()-7), DAY(GETDATE()-7)))//7 days before starting at midnight
AND
(ARRIVAL_DATE<DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), DAY(GETDATE()))) //NOW in the YYYY, MM,DD format
This will get everything that happened in the current calendar week (Mon-Sun)
SELECT * FROM Table1
WHERE ArrivalDate BETWEEN
CAST(DATEADD(dd,(((DATEPART(dw,getdate())+##DATEFIRST) % 7)+5) % 7 ,getdate()) as date) AND
CAST(DATEADD(dd,6+((((DATEPART(dw,getdate())+##DATEFIRST) % 7)+5) % 7 ),getdate()) as date)
Edit - Removed extra E in BETWEEN

Modify SQL Row to set back date

I am currently running a PHP script that is grabbing data from Facebook and putting it into my SQL database. I want to now be able to see what Facebook posts were posted within 2 hours of the data grab, and I realized that the NOW() command was making a time that was 4 hours ahead of the offset for the Facebook server.
This brings me to three questions:
1) Is there a way to do a select command where I can offset the date by 4 hours?
2) Is there a way to modify rows to offset the date by 4 hours?
3) Will these methods actually set the entire date back, so that if a date is, let's say at 1:00 today (March 13th), it will be modified to be 21:00 the day before (March 12th).
Thanks so much!
Any place you use a date in SQL you can use the DATEADD function, in a select, in an insert and in a where
DATEADD(hour,4,datevalue)
To go back just use a negative number
DATEADD(hour,-4,datevalue)
So, this
DECLARE #datetime datetime = '2013-03-13 01:01:01.110';
SELECT DATEADD(hour, -4, #datetime);
returns this:
2013-03-12 21:01:01.110
1) Is there a way to do a select command where I can offset the date by 4 hours?
Yes, using DATEADD/DATE_ADD
2) Is there a way to modify rows to offset the date by 4 hours?
You shouldn't be modifying the data.
3) Will these methods actually set the entire date back, so that if a date is, let's say at 1:00 today (March 13th), it will be modified to be 21:00 the day before (March 12th).
If you're using the DATEADD/DATE_ADD function and your date is in a date type, then yes.
SQL Server Manual
MySQL Manual