Selecting records assuming future dates in mysql - mysql

I used the following select statement to select sum of amount assuming no future dates. How do I change this statement to include future dates?
SELECT SUM(amount) as allPreviousAmount
FROM `fn_table`
WHERE MONTH(transdate) < ? OR YEAR(transdate) < 2019 )
The ? is representing the month number. I have data in 2018, 2019 and 2020. Now I want to select all data before February 2019. How do I select it? The problem is that this also select data in 2020.

Regards your existing query, surely you'd need AND, not OR, to ensure no future dates? Say it's July 2020 and you have a date of January 2021 which is in the future. If you say MONTH('jan-2021') < MONTH('jul-2020') OR YEAR('jan-2021') < YEAR('jul-2020') then this will be true because jan is earlier than jul so a future date of January 2021 will be returned
Not really sure why you didn't just WHERE transdate < CURRENT_DATE() unless a any day this month is also classed as a future date... (by demanding the the month and the year have to be less than the current date, if it's 13th July 2020 you'll only get records up to end of June 2020 if you consider month and year only)
But to include all dates past and future simply remove the WHERE clause entirely
Edit in response to comment:
If you want a query that gives "all data up to but not including 01 Jul 2005" then do:
WHERE transdate < ?
And in your front end supply a date type parameter of 01-jul-2005. Any date can be supplied, but to cut off at a particular month end, supply a date that is the first day of some month
You can reuse the same query for future dates too by providing a date higher than any possible date in the table, such as 01-jan-3000

Related

Is there any function in sql to extract week?

I want to extract week from datetime, the output I want is 'YY/week', where week is the week of the year (eg '201724' is the 24th week in 2017).
The term "week of the year" is too ambiguous.
The week may start from Sunday, Monday or another weekday
The weeks enumeration in the year may start from 0 or 1
The weeks enumeration in the year may start from the week which includes January, 1 (and hence may be partial) or from first complete week of the year
The last week of the year, if it is partial, may be counted or not
Each DBMS has its own functions (sometimes original, always with original names) that can return the number of the week in the year on a given date. But they can not always take into account the above features.
Important addition provided by jarlh:
ISO 8601 (#4.3.4):
The first calendar week of a year is the one that includes the first Thursday of that year.
The last calendar week of a calendar year is the week immediately preceding the first calendar week of the next calendar year.
Week 1 is the first week of a year.
A calendar week starts on a Monday.
ISO 9075 doesn't even mention weeks.
SELECT TO_CHAR(TO_DATE('19-FEB-22') , 'IW') from DUAL;
To get the corresponding four-digit year, use
SELECT TO_CHAR(TO_DATE('19-FEB-22'), 'IYYY') FROM DUAL;
TO_CHAR() having so many options like this read more in Oracle manual or extract portation of date Extract Portion of Date Time Value
OutPut
You can use the following Mysql type query to extract.
SELECT DATE_FORMAT(BirthDate, " %u %Y") FROM Employees;
where the BirthDate date column in the database and the Employees is the table name.
This will result
49 1968
08 1952
35 1963
Week and the year.
in postgresql:
SELECT to_char('2016-12-31 13:30:15'::timestamp without time zone, 'yy/ww') ;
result:
16/53

Mysql query to fetch/store records in a specific month

I have this module where i am supposed to check the winners of bidding in the current month. be that October, November, December or whatever.
I run this query,
SELECT *
FROM auction_winners
WHERE MONTH('2015-10-16 00:00:00')
but it shows me everything, from all the months.
i believe even if this query works it will work for October only, i am looking for something that checks system current date month.
Just add your column with date:
SELECT * FROM auction_winners WHERE MONTH(column_name) = MONTH('2015-10-16 00:00:00')
or:
SELECT * FROM auction_winners WHERE MONTH(column_name) = MONTH(NOW())

Selecting rows with a set date+a month offset with MySQL

I have a MySQL database with one table that contains a data field and a "period" field, in months - int.
The idea is that the date indicates a due date to begin a project inside my company. And the "period" the period of time it is suppose to take to finish it, in months.
I need to select rows that will impact a given year. So if I am generating a report for 2014, I need to select the rows such: date+period is inside 2014.
It will be easy to do it inside the program, but I am looking for a way to do it in the query - if possible.
So basically I just need a way to sum dates and ints in a query, where the int is the number of months.
Any thoughts?
It's easy to do date arithmetic in MySQL and other RDMS systems. You need all the records in which the start date is not after the year in question OR the end date is not before the year in question. That is this expression:
NOT(YEAR(start_date) > 2014 OR YEAR(start_date + INTERVAL period MONTH) < 2014)
This logically reduces to
YEAR(start_date) <= 2014 AND YEAR(start_date + INTERVAL period MONTH) >= 2014
So this query will do it.
SELECT whatever, whatever
FROM project
WHERE YEAR(start_date) <= 2014
AND YEAR(start_date + INTERVAL period MONTH) >= 2014
AND (whatever other selection criteria you have)
This will give all projects that were active during 2014, including those that started before 2014 and those that will still be in progress at the end of that year.

How to check if date is in range regardless of the year

I'm wondering what would be the easiest way in MySQL to check if given date is in range regardless of the year.
In database table I have two DATE fields: start and finish stored in YYYY-mm-dd
if start = 2013-11-01 and finish = 2014-03-01 anything between 1st of November and 1st of March of any year should be accepted.
Valid dates:
2020-01-01 1980-02-28
Invalid dates:
2013-10-30 1968-07-30
There are almost certainly cleaner ways of doing it, however this should work:
((DAYOFYEAR(finish_date) > DAYOFYEAR(start_date)
AND (DAYOFYEAR(#date) >= DAYOFYEAR(start_date)
AND DAYOFYEAR(#date) <= DAYOFYEAR(finish_date)))
OR (DAYOFYEAR(finish_date) <= DAYOFYEAR(start_date)
AND (DAYOFYEAR(#date) >= DAYOFYEAR(start_date)
OR DAYOFYEAR(#date) <= DAYOFYEAR(finish_date))))
For a start date in Oct 2012 and end date in Nov 2020 this will return all dates in the Oct-Nov range. If in fact would want it to return all Dates when the range is greater than a year (and hence covers all dates of the year) you could add:
OR DATEDIFF(Day, start_date, finish_date) > 356
before the final bracket.
use DAYOFYEAR:
When the Start Date is earlier in the year than the Finished Date:
the tested Date should lye between Start Date and Finish Date (or on Start or Finish)
When the Finished Date is earlier in the year than the Start Date:
the tested Date should lye outside the Start Date and Finish Date (or on Start or Finish)
You can use some date extract function and then check your condition..
for example.
SELECT EXTRACT(MONTH FROM TIMESTAMP '2013-11-01 20:38:40');
this will give ouput start month as 11
SELECT EXTRACT(MONTH FROM TIMESTAMP '2014-03-01 20:38:40');
this will give ouput end month as 3
now you can check the condition from above two result..
SELECT * FROM tableWithDates t WHERE month(t.start) >= 11 AND month(t.finish) < 3
if you want the first of march it will go like this:
SELECT * FROM tableWithDates t WHERE month(t.start) >= 11 AND (month(t.finish) < 3 OR month(finish) <= 3 AND day(finish)<=1)
Depending on the size of the data you will run this at. You can get into performance problems, as MySQL can't use indexes of calculated columns.
If you run into this i suggest spitting the month AND/OR day into separate columns.
Edit:
Given an one parameter input as '2008-02-29'
SELECT * FROM tableWithDates t
WHERE
month(t.start) >= month('2008-02-29') AND day(t.start) >= day('2008-02-29')
AND month(t.finish) <= month('2008-02-29') AND day(t.finish) <= day('2008-02-29')

My SQL query to list dates that have no records

I have an online calendar system that I use for tracking my band's gigs - I'd like to construct a query that will display all Fridays and Saturdays that don't currently have a record assigned to them.
eg,
if I have a record in the DB for Friday 23rd Aug and Friday 30th Aug (records being gigs that are booked), what would the query cirteria be to output Saturday 24th Aug (as it has no record)?
Select * from ['giglist']
where ['gigdate'is in 'friday','saturday']
and ['gigdate' doesn't have a record]
I will probably set the days of the week as variables so that the user can run the query for any day or selection of days.
Thanks,
Darren
if assuming from your question there is a field gigdate of date type that keeps date information and a seperate record field.
Then query would be,
select DAYNAME(gigdate), DAYOFMONTH(gigdate), MONTHNAME(gigdate) from giglist where
DAYNAME(gigdate) in ('Friday', 'Saturday') and
recordfield is NULL;
It's better to use single date type field and just store date only, as mysql has powerful set of date functions to help you out for your needs.