How to do a month search by sql query - sql

I have 1 table in MS Access which is like:
id date
1 1/01/2016
2 1/01/2016
3 1/01/2016
How do I perform a search operation on it by month - meaning a search where the month is January/February?

Using dates as criteria in Access queries:
Contain a date within a specific month (regardless of year), such as December
DatePart("m", [SalesDate]) = 12
Returns items with a date in December of any year.
Where of course you replace [SalesDate] with your column...

Related

Oracle: Date Range

I have the below Query & table:
Problem:
I'm trying to capture IDs that were Reassigned for that month.
For example the month of August total number of ReAssigned would be 1 and not 3
How can this be captured?
I need to count Per Month, for example. How mamy ids were reassigned for the month of August, Sept, ect.
So, the Most_Rent_Assigment date needs to fall within the month
Aug: 8/1-8/31
Sept: 9/1-9/30
Then count to capture how many ids were reassigned in that month
,CASE WHEN
H.FIRST_ASSGN_DT IS NULL THEN 'UnAssigned'
WHEN h.TOTAL_NUMBER_OF_REASSIGNMENTS = 0 OR h.TOTAL_NUMBER_OF_REASSIGNMENTS IS NULL
AND h.FIRST_ASSGN_DT IS NOT NULL THEN 'NewlyAssigned'
ELSE 'ReAssigned'
END ASSIGNED_STATUS
Table:
ID Total_Number_Reassignment Most_Recent_Assignment StartDate *Assigned Status
1 2 11/01/2016 08/1/2017 ReAssigned
2 3 08/02/2017 08/01/2017 ReAssigned
3 0 08/15/2017 NewlyAssigned
4 5 12/01/2016 09/01/2017 ReAssigned
Date ranges in Oracle can be accomplished by using BETWEEN.
If the columns are type DATE then you can just do: WHERE TRUNC(Most_Recent_Assignment) BETWEEN TO_DATE('2017-08-01') AND TO_DATE('2017-08-31')
Note: the TRUNC will truncate the date to midnight, so you don't lose results based on time of day.
Edit:
OP clarified his question.
To count occurrences for a particular month, you can group by month.
SELECT
TO_CHAR('Most_Recent_Assignment', 'mm'),
COUNT(TO_CHAR('Most_Recent_Assignment', 'mm'))
FROM TABLE
WHERE ...
GROUP BY TO_CHAR('Most_Recent_Assignment', 'mm')
TO_CHAR parses a date as a string. mm will return a 2-digit month. You can replace it with Month to get the name of the month.
The group will put all months together.

SQL check if date exists in the next 2 years

I have a table where each record has multiple dates saved in the date column such as Record ID '01' can have 6 rows with dates each from 2015, 2016, 2017, 2018 and 2019. Each record ID only has one date per year.
I am interested to see for all record ID that have a date in the current year, if they have a date in the next 2 years (i.e Record ID has a date from 2017 and 2018), just the next year (Record ID only has a date from 2017) or just this year.
I am not interested in records that do not have a date in the current year even if they may have dates in the next 2 years. I am also not interested in records that may have a date 3 years from now but not have a date in the next 2 years.
Thanks in advance.
Based on your question. Try this.
Updated.
Select RecordID, Year(DateColumn)
From table
Where Year(DateColumn) Between Year(GetDate()) and (Year(GetDate()) + 2)
Group By RecordID, Year(DateColumn)
Having Count(1) = 3

Earliest and Lastdate for each year in sql

I have a column with 3 columns. I have multiple records for a year. As you see some of my records as follows
ID stardate enddate
1 1/1/2010 5/3/2010
2 2/4/2010 NULL -**EDIT**
3 1/2/2011 5/6/2011
4 3/4/2011 NULL -**EDIT**
I want to get a result for the earliest date in that year and the last date in that year. So output could be like
**EDITED:** 1/1/2010 12/31/2010 - For Year 2010
**EDITED:** 1/2/2011 12/31/2011 - For Year 2011
How can i get that in a query?If you need more info,please ask. Thanks
EDIT: If for the year if one of the columns read NULL then I have to consider the last day of the year as the enddate. i.e.12/31/YYYY. And I need to do that for each year again.
Assuming you use DATE (or related) columns in a MySQL table, something like this should serve your request:
SELECT MIN(startdate),
MAX(enddate),
YEAR(startdate)
FROM my_table
GROUP BY YEAR(startdate);
This groups all entries by year (of the startdate) and show you the minimum and maximum entries for each year as you want. See also the documentation for the DATE function in MySQL.
There are similar date functions and possibilities if you are using an other database system. Usually you can easily find them by googling the database system and something like "date functions".
select MIN(stardate),max(enddate)
from [Tablename]
where YEAR(enddate)=2013

Compare date and month in sql server for alert system

I have an a table with two columns birthday and anniversary. I want to get alerts about birthdays and anniversaries between a 7 day period of time but, that should not include year (obviously if I include year, it would always be less than the current date). I want to get the alerts 7 days in advance.
That is, the query should compare the birthday and anniversary with the current date and return a list if their birthday or anniversary falls between 7 days of the same month so that it alerts me in advance about the upcoming birthdays and anniversaries.
Subtract the year difference from now to the requested date and then use datediff to calculate the date difference of the result with the requested date.
SELECT *
FROM Table
WHERE DATEDIFF(dd,DATEADD(yyyy,-DATEDIFF(yyyy,Birthday,GETDATE()),GETDATE()),Birthday) BETWEEN 0 AND 7
OR DATEDIFF(dd,DATEADD(yyyy,-DATEDIFF(yyyy,Anniversary,GETDATE()),GETDATE()),Anniversary) BETWEEN 0 AND 7
Try This
SELECT Name,max(Table .birthdate)
FROM Table group by Table .Name having (datediff(day,max(birthdate),getutcdate())>7 and datediff(day,max(birthdate),getutcdate())<8)

Select dates within the last week

I want to perform an operation in crystal report.
I have a db table contains a date column.
I want to filter and get the rows having data created in last week(last sunday to last saturday = 7 days).For example if today is 24th August Wednesday, then I need data from 14th August(Sunday) to 20th August(Saturday).
Basically I want to find 2 dates and filter the date column.
Date1 = Date(CurrentDate)-Day(7 + WeekDayinNum(CurrentDate)) ; (Ex:for my example it will be 10)
Date2 = Date(CurrentDate)-Day(WeekDayinNum(CurrentDate))
I do not know the Date APIs properly,can anybody help me in this.
This is a common enough date range that CR provides it for you. In your record selection formula, you can just add {table.date} in LastFullWeek
From CR, "LastFullWeek specifies a range of Date values that includes all dates from Sunday to Saturday of the previous week."
Add this to the record selection formula:
{table.date_field} IN Last7Days
If today is Sunday(1) you want rows that are between 7 and 1 days old,
If today is Monday(2) you want rows that are between 8 and 2 days old,
If today is Tuesday(3) you want rows that are between 9 and 3 days old,
etc.
SELECT *
FROM `tablename`
WHERE `somedatefield` >= DATE_SUB(NOW(),INTERVAL (DAYOFWEEK(NOW()) + 6) DAY)
AND `somedatefield` <= DATE_SUB(NOW(),INTERVAL (DAYOFWEEK(NOW())) DAY)