SQL query for removing current and future dates? - sql-server

I would like to filter out all records that have a date in the future AS WELL AS any records in the current month. I attempted to use this query:
...
WHERE (Month(DueDate) < Month(getdate()) AND (Year(DueDate) <= Year(getdate())))
The issue is that it doesn't return any results that were previous year but a month that is greater than the current. ie: Today's date is 9/12/18, it would not return any records that had a month of 10, 11 or 12 regardless of the year.

This will filter off any records with a DueDate after the first of the current month.
WHERE DueDate < DATEADD(MONTH ,DATEDIFF(MONTH, 0, GETDATE()), 0)
EDIT: I had >. Should've been <.

Try something like:
SELECT ...
FROM ...
WHERE DueDate < DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 1);
This solution uses DATEFROMPARTS function for which you can see more information in the documentation. The idea is to get the first of the current month for the current year and select only those rows whose DueDate is before the given date and time.
Also, instead of GETDATE() (link) function you might want to check out GETUTCDATE() (link) if it's more suited to your needs.

Related

T-SQL show records that are due to expire in two months

How can I show records that are due to expire in two months?
I have tried using DATEADD but I appear to have the logic code incorrect.
GETDATE() >= DATEADD(MONTH,-2, cycles.[NEXT-DATE])
Any code on how I can do this?
Thanks,
Your code gives you records that have expiry date equal 2 months or more. You should change your condition. Draw a time line to visualize it (it's easier then to understand how it should look like).
GETDATE() >= DATEADD(MONTH,-2, cycles.[NEXT-DATE])
and GETDATE() <= cycles.[NEXT-DATE] --checking if date is in the future
I guess it should be sth like this
DATEDIFF(DAY, DATEADD(MONTH, 2, GETDATE()), cycles.[NEXT-DATE]) < 0

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)

Filtering columns based on a date in a field for current week, past weeks, and future weeks

Example Table:
(Column Heading) Total Interviews of everyone --- (Row Heading) Interview Requested
(Column Heading) Number of interviews this week -- (Row Value) Count all the dates scheduled this week that have the status: "Interview Requested" e.g 4
(Column Heading) Number of interviews next week -- (Row Value) Count all the dates scheduled next week that have the status: "Interview Requested" e.g 6
First of all i'll like to aploigise about my makeshift table, it seems that i cant use pictures since im a new member.
I am also new to using reportbuilder and sql and have been trying to figure out how i can count the amount of dates that fall between certain date ranges in a report.
Like ive stated in the makeshift field called "Number of interviews this week" I wish to count all the dates scheduled for the current week, the week after, the week before and so on based on a date field. I then plan to break this table down to show all the interviews a single person has schedueled.
The issue I am having is that there will be many dates spanning different months/days. So Im not sure how i can represent this with DateDiff or other date features, because the table is meant to be a live representation of the current interviews.
Is what i'm trying to do even possible using report builder?? If so any tips would be great, if not then thanks for taking the time to look at my question.
As Requested Dataset Fields:
[UserName] (User table), [InterviewStatus] (Interview table), [DateUpdated] (Interview Table)
Those are the main ones that will be used in the report
If you just want to count the number of columns of each field, you can use the sql statement COUNT() see COUNT().
You can do the common query like this:
SELECT COUNT(fieldName1), COUNT(fieldName2), ... , COUNT(fieldNamen) FROM <tableName>
Also try to show your codes so that your question will be more clarified.
You can use the generic select statement from your interview table filtering in the where clause for dates between the beginning of the current week and the end of the next.
Then the two fields would be a sum of 1 if the date falls within the current week or next.
EDIT:
Example below will give the number of dates within this week. Taken from another question:
Select InterviewStatus,
Sum(Case when DateUpdated >= DATEADD(dd, -(DATEPART(dw, GETDATE()) -1), GETDATE())
AND DateUpdated < CAST(CONVERT(VARCHAR(10), DATEADD(dd, (8 - DATEPART(dw, GETDATE())), GETDATE()), 120) AS DATETIME)
Then 1
Else 0 end)
as NumberInCurrentWeek
From InterviewTable
Where DateUpdated >= DATEADD(dd, -(DATEPART(dw, GETDATE()) -1), GETDATE())
AND DateUpdated < CAST(CONVERT(VARCHAR(10), DATEADD(dd, (8 - DATEPART(dw, GETDATE())), GETDATE()), 120) AS DATETIME)
Group by InterviewStatus
This just needs to be tweaked to your exact circumstance. For example alter the 8 by 7 will capture the current fortnight.
Then produce a simple table based on the query.

SQL Query-calculating last week data for an ordinary calendar year

I have to retrieve the last week data from a table.
i am using the following condition.
#prmCurrent_Year=Datepart(year,getdate())
last_week=case when Datepart(week,col_name)=1
then 52
else Datepart(week,col_name)-1 and
year_num=case
when Datepart(week,col_name)=1
then #prmCurrent_Year-1
else #prmCurrent_ Year
will this work properly or is there any other better query for this???
Something like this might suffice:
SELECT *
FROM Table
WHERE MyDate BETWEEN DATEADD(wk, -1, GetDate()) AND GetDate()
DateAdd reference on MSDN: http://msdn.microsoft.com/en-us/library/ms186819.aspx

Delete records based on dates

I have a database "DB" and I want to delete records from table "MyRecords" which are of future ("RecordDates" which are more than today just to avoid date change on system) and less than x no. of days from today.
That "x" may vary. Moreover, I want to execute same type of command with month and year also.
I am using SQL Server and C#.
To delete records which are in the future and less than n days in the future you could use T-SQL such as this:
DELETE FROM DB.table WHERE
(date > GETDATE() AND date < DATEADD(day, 5, GETDATE()));
In this case n is 5.
You can see all the arguments to DATEADD() at DATEADD (Transact-SQL)
This query will delete all records that are later than today's date, but less than 30 days in the future. You could replace "30" with a variable so you could determine how many days in the future to delete.
DELETE FROM Table
WHERE
TABDate > GETDATE() and TABDate < DATEADD(day, 30, GETDATE())
UPDATE
To delete all records less than 30 days in the past, you would change the query to look like this:
DELETE FROM Table
WHERE
TABDate > DATEADD(day, -30, GETDATE()) AND TABDate < GETDATE()
Also note that all these examples are calling GETDATE() which also has a time component as well as a date, so care must be taken in that anytime you see a statement like < GETDATE() you are not just deleting records with a date before, say, 2011-09-29, you are deleting all records with a date before '2011-09-29 17:30'. So be aware of that if you table dates contain times as well.
You can use the query DELETE FROM DB.table WHERE date > now() or WHERE date > unix_timestamp(), depending on how you are storing your dates. (i.e. date-time vs. timestamp).

Resources