Extract date mysql - mysql

as a very noob on mysql, I'm wondering how I can get my invoices, dated since more than 16 years.
I need a function that works at all time, so without specifying today's date.
I know it starts by
select extract(year from now()...)
the "..." being the rest of the function.
Here' the table structure
I expect the script to fetch all the invoices stored in the database, that are dated since more than 16 years
If you guys need more details I'll be glad to give.
Thanks !

To get the year 16 years ago:
extract(year from current_date - interval'16' year)
ANSI SQL compliant way, supported by MySQL.

Related

Add one date to date in MySql

I am setting up a MySQL (Maria) where I am going to upload demo data for an application. In the demo date there will be many different dates from the last 6 months.
What I need to do is to keep all dates the same number of days from today all the time. So a date that is x number of days earlier or later than today keep that interval constant.
The demo data will be used in a PHP application so it is maybe an option to run PHP code to change the dates every day, or is it best done any other way?
I have been trying to just add +1, but it does not work on dates.

How to find the difference in terms of days from YYYY_MM_DD formatted dates in bigsql?

I want to find the difference between two dates in YYYY-MM-DD format. For example, I have 2 dates 2018-10-31 and 2018-11-07. I want to find the difference in number of days between those two dates (7 days) in BigSql. I have gone through stack overflow and other websites but couldn't find anything that works as there are very few resources for BigSql.
I have tried below query which works fine for sql server but not for bigsql :
SELECT DATEDIFF(DAY, '2018-10-31', '2018-11-07')
Any help would be appreciated
Thanks
Finally after continuous efforts I found something that worked for me posting here if in case someone looking for the the same:
SELECT DAYS_BETWEEN ('2018-10-31', '2018-11-07),

Mysql query group by week with specific start and end

I searched stack overflow and have come so close yet have not found if this is possible.
I want to group a weekly query from 11:30 am Friday to 11:29 am Friday for each week.
adding days and hours only partially work. I need to keep this in a mysql query not coding in php, etc.
You need to give more information on your table structure and some sample date. If I have understood correctly then below is simple pseudo code which you can start use to start framing your requirement .
select something from table where date between 1:30 am Friday and 11:29 am
group by trunc(date).
Note :
- Format the date in where condition using to_Date.
- Week definition might be different in database so you need to manipulate.

Using MySQL to return row between to strings with date, but without year

I am currently working on a project in which I want to store commemorative days (like January 8th's World Leprosy Day) in a database. At this moment they're stored in a table which contains:
- an ID
- the date as varchar (stored European style, e.d. "8-01" for January 8th)
- length of the commemorative day (as some span multiple days)
- and the name
The reason I am storing the date as varchar is because the year is irrelevant, and I'm a bit reluctant to just store a year (e.g. 2013) in the database and truncate it.
But here's the problem: I can't seem to find a way to construct a query that will get the rows between dates. I think it's because the way the dates are stored in the database.
I already tried (given day = "8-01")
SELECT * FROM comdays WHERE date(day) BETWEEN date("1-01") AND date("20-01")
But to no avail.
Is there a way to get this thing going with strings? Or do I have to change the date column into a MySQL DATE format?
Thanks in advance!
If you really want to keep non standard date field in MYSQL you will need to use the following format 0108-> mmdd this format allows calculations.
It might also be worth reading the following answers to similar question Save day and month in database

Sorting by month in MS Access

I am using an MS Access db to track some tasks during the year. Each task has a due Month. I do not want to use exact dates as the convention in my team is to refer to the month. I don't want to store the dates in a date format as team members will be entering the due month by hand.
Is it possible to sort my fields in date order if the date is stored as a text string month? (eg. January, February rather than 31/01/2009, 28/02/2009).
If so, what would such a query look like?
Thanks in advance.
If you are storing only the month name, your will first need to convert to a date to get a month number, use a lookup table (MonthNo, MonthName) or use the Switch function. Here is an example of converting to a date:
SELECT Month(CDate(Year(Date()) & "/" & [MonthNameField] & "/1")) AS MonthNo
FROM Table
However, there is probably a good argument for storing a date based on the month name entered, this would prevent any confusion about years.
This should work
SELECT *
FROM TableName
OrderBy Month(date_field)
I would store the month as an integer 1-12 then you can easily sort them.
I would make a date field.
I would store 1/1/2009 for January 2009, 2/1/2009 for February 2009, and so forth. For display purposes, I'd format it so that it displayed only the month (or Month + Year -- can't imagine how you wouldn't want the year).
This makes it possible to take advantage of date operations on the field without messy conversions of text to date formats.
Thank you all for your responses. Sorry for the delay in responding - I'm working on this issue again now.
For clarity, the DB is to be used to track a schedule of events within a 12 month period. The year does not need to be stored as everything in the DB is referring to the same year. A new copy of the DB will be made at the beginning of 2010.
I'm really keen to actually store the month as a word rather than any kind of value or date field as when bulk adding tasks I will likely edit the table directly rather than use a form.
I realise this is dead but google brought me here while i was looking so thought I would add to it:
I had this problem myself (Access 2010) and found a decent answer here: http://www.vbforums.com/showthread.php?503841-How-to-Convert-MonthName-to-Value(Microsoft-access-2003)
So what I did was have a Query which pulled out the DISTINCT months from my table. Then in the design view i added another column with MonthNo: Month(CDate("1 " & [Month]))and sorted the query on this column
hope this helps someone if not the OP.