Mysql accepts Zero month in date column!? Please explain - mysql

MySQL 5.6 - we had a talk with one of the developers and found something very curious - couldn't find anything about it in google:
create temporary table md (dates date);
insert into md select '2000-01-01'; -- result: 2000-01-01
insert into md select '2000-00-01'; -- result: 1999-12-01 ????
insert into md select '2000-00-00'; -- result: 1999-11-30 ????
insert into md select '2000-01-00'; -- result: 1999-12-31 ????
insert into md select '2000-13-01'; -- result: EXPECTED: SQL Error [1292] [22001]: Data truncation: Incorrect date value: '2000-13-01' for column 'dates' at row 1
insert into md select '2000-12-49'; -- result: EXPECTED: SQL Error [1292] [22001]: Data truncation: Incorrect date value: '2000-12-49' for column 'dates' at row 1
Could someone please explain?
Why does it let this happen? It should throw the same Data issue error imho.
Documentation?
What is the exact logic behind this?
a) Month 00 previous month same day?
b) Day 00 previous month last day?
c) both 00-00 previous month's previous' last day?
Thanks!

https://dev.mysql.com/doc/refman/8.0/en/using-date.html
MySQL permits you to store dates where the day or month and day are zero. This is convenient if you want to store a birthdate in a DATE column and you know only part of the date. To disallow zero month or day parts in dates, enable the NO_ZERO_IN_DATE mode.

Related

SQL Query to get data between two weeks?

I have a week column with week numbers as w0, w1, w2.... I am trying to get last last six weeks data. Here's the sql query I am using.
SELECT * FROM week
WHERE uid = '9df984da-4318-1035-9589-493e89385fad'
AND report_week BETWEEN `'w52' AND 'w5'`;
'w52' is essentially week 52 in December 2015 and 'w5' is Jan 2016. The 'between' seems to not work. Whats the best way to get data from the above two weeks?
Here's the CREATE TABLE statement:
CREATE TABLE `week` (`uid` VARCHAR(255) DEFAULT '' NOT NULL,
`report_week` VARCHAR(7) NOT NULL,
`report_files_active` BIGINT DEFAULT NULL);
Essentially this table is getting populated from other table which has date column. It uses dates from other table and summarizes weekly data into this.
Any help is appreciated.
Refer to this SO Discussion which details the reasons for a problem similar to yours.
BETWEEN 'a' and 'b' actually matches to columnValue >='a' and columnValue <= 'b'
In your case w52 is greater than w5 due to lexicographic ordering of Strings - this means that the BETWEEN clause will never return a true (think about it as equivalent to saying BETWEEN 10 and 1 instead of BETWEEN 1 and 10.
Edit to my response:
Refrain from storing the week value as a string. Instead here are a couple of approaches in order of their preference:
Have a timestamp column. You can easily then use MySQL query
facilities to extract the week information out of this. For a
reference see this post.
Maintain two columns - YEAR, WEEKNO where YEAR will store values
like 2015, 2016 etc and WEEKNO will store the week number.
This way you can query data for any week in any year.
please show me table structure and DB name because it different for other, if it is any timestamp then we can use BETWEEN 'systemdate' AND 'systemdate-6'

MySQL query to calculate difference between two days and insert that value into column in same table

I have considered a start date as '11th July 2014 2:00 AM' (explained below). I have other column in same table where I have values stored in the format "YYYY:DD:MM Min:Sec". I want to subtract each entry from this table from start date and insert it into new column called 'Day'. Here is the query I am trying to execute:
set #start = '2014-07-11 02:00:00';
use rawlog;
insert into api_raw(`day`)
VALUES
(date_diff(DAY, start, #column_name)
I do not understand the error I am getting. Where I am going wrong in this one? Any help would be appreciated. Thank you.
use
DATEDIFF (DATE(#start),DATE(#column_name))
rather than
date_diff(DAY, start, #column_name)

Filtering the dates range based on the days

I have a table with columns as
Id (1)
Date holding values as (2013-12-12 00:00:00)
Start time (00:00:00)
end time (01:00:00)
value
The user will give the date range and will specify day like sunday ,monday etc .
How can I use a sql query to filter the dates and find the proper days between that for the specified days as well.
One way would be to use BETWEEN ... AND ... and DAYOFWEEK():
SELECT *
FROM my_table
WHERE Date BETWEEN ? AND ?
AND DAYOFWEEK(Date) = ? -- 1=Sunday ... 7=Saturday

MySQL display rows with timestamp less than 07:00

I have a column in my SQL database that has values like 1:00 (meaning 1 AM), 13:56 (1:56 PM), etc. I would like to have the mysql command show only the variables after 7 AM. Is that possible? Thank you!
Presuming your column is named theTime, in a table named foo:
select * from foo where HOUR(theTime) >= 7;
If your column is not a date/time or timestamp field, you are in some trouble.

MySQL Return Today's Records Using DATE from DATETIME Field

I have a table called "actions" with a DATETIME column called "occurred". I'm trying to return the records for today by doing the following
SELECT * FROM `actions` WHERE `occurred` = DATE(NOW());
But I get an empty result set. If I take the WHERE clause out, I can see all 295 rows in the table and there's at least 30 rows from today. Later I will be writing another query to return all records between today's date and X amount of days in the past but before I can get there I need to know why this query is returning an empty result set.
Thanks in advance.
SELECT * FROM actions WHERE DATE(ocurred) = CURDATE();
DATE(ocurred) ignores the time part.
Here's the SQL Fiddle to play with the data: http://www.sqlfiddle.com/#!2/81708/2
If there in no future date in occurred, you could just use below:
SELECT * FROM `actions` WHERE `occurred` > DATE_SUB(CURDATE(), INTERVAL 1 DAY);