I have a project for travel agent where I need to insert date that support incomplete date. So there are 3 possible format:
YYYY-MM-DD
YYYY-MM-00
YYYY-00-00
00 means month or day is not set.
In my database (MariaDB v10.1.28) I can run this query and it sets the date as I wish:
UPDATE IGNORE departure SET flight = "2019-00-00" WHERE id = "10"
I use IGNORE to bypass strict mode in config/database.php. In Laravel, when I tried to run this raw SQL query:
DB::update(UPDATE IGNORE departure SET flight = ? WHERE id = ?', ['2019-00-00', '10']);
said record will have its departure changed to 0000-00-00. Is this a bug? There is a workaround to use 3 tinyInt but changing structure might mess everything else up.
Related
MySQL version: 8.0.23-0ubuntu0.20.04.1 - (Ubuntu)
When running sample query:
SELECT * FROM `redacted-tbl`
WHERE `redacted-col` = 'some-invalid-date'
ORDER BY `redacted-col` DESC LIMIT 0, 25
data structure: redacted-col DATE
I'm getting #1525 - Incorrect DATE value: 'some-invalid-date' error.
Now I understand that 'some-invalid-date' is definitely not a valid mysql date format. I understand that the error is expected behavior if it's an INSERT or UPDATE query.
But why do I get such error on SELECT query? Previous version of mysql didn't throw such error for SELECT query (only for INSERT/UPDATE).
Also, how do I turn off this error for SELECT-ing DATE column? Is there any mysql flags to disable such check?
Edit (added from my comment):
In my opinion, there are good reasons to allow comparison of non-valid-date-string with DATE columns:
querying with WHERE mydatecol > '2015' to get all date that is after '2015-01-01'
even better, I can just pass user inputted date as filter (sanitized and parameter-bind-ed of course): WHERE mydatecol > ?,
if user enter 2015 then it will become shorthand for user who cares only to get all records after 2015
if user enter 2015-04, then it will become shorthand for user who want records after 2015 month 04/April)
if user enter 2015-04-15 (normal/valid mysql date string), then app will display records after 2015 month 04/April date 15
without this "non-date-validated comparison", I would have to write more application code just to check if the user inputted valid date or not, e.g.:
if the input format is 2015 then I have to change it into 2015-01-01,
else if the input format is 2015-04 then I have to change it into 2015-04-01,
else if the input format is 2015-04-15 then it's valid,
else it's not valid and throw error (or just output current date/default date or just show 'no entry matched your search criteria')
[The text of this answer was originally written by forpas https://stackoverflow.com/users/10498828/forpas ]
You can cast mydatecol to a string to perform the comparison. An easy way to do it is with CONCAT():
WHERE CONCAT(mydatecol) > '2015'
or with cast:
WHERE CAST(redacted-col AS CHAR) > 2015
I have a sql file containing bYear and u_age column in users table.
I would like to know how I can change all the digits in bYear, such as 1986, 2000, to u_age such as 33, 19.
Thanks so much !!
If you are looking to update the table (not the file), you can just do:
update users set u_age = year(curdate()) - bYear;
curdate() gives you the current date, from which you can extract the year using the year() function.
Please note that this computation is not accurate at all: to compute an age, you need the entire date of birth (including month and day). The above computation behaves like the date of birth is actually the first day of year bYear.
If you are looking to update a sql file: as commented by Raymond Nijland, just don't. This is much more complicated and far less efficient. Instead, load the file in a table, update the table and then export it to a file
I'm connecting from MS SQL Server 2014 to a (ServiceNow) MySQL database via OpenQuery(). I would like to filter out records more than 24 hours old.
When I set a static date, it returns the thousands of rows I expect to see. However, when I try to use a calculated field, it runs but returns zero records.
select number, sys_updated_on
from OPENQUERY(ServiceNowUAT,
'Select number, sys_updated_on
FROM DATABASE.[SCHEMA].[TableName]
WHERE sys_updated_on > DATEADD(d, -2, NOW()) ')
I have also used the DATE_SUB() function, and various other forms of syntax. I've tried casting the calculated date as date, datetime, timestamp, varchar, and more. I've tried this in MS Query and SSIS as well. All fail to return results with this query, and other, similar queries once I add the "sys_updated_on > DATEADD(d, -2, NOW())" segment.
If I cast the sys_update_on field as timestamp, it works, but cranks up the processing time from about 10 seconds to 30+ minutes, which, of course, is not ideal (there are a few million rows in the table
The sys_update_on field is in the format "2015-02-10 10:24:17.000000".
The other relevant part is that I am pulling from a ServiceNow MySQL database using ODBC drivers provided by ServiceNow, not MySQL. I do not have a data map, so I cannot say for sure what the data type is. At this point, I'm guessing it's a string of some sort, and not a true timestamp/datetime, but I can't confirm this.
Does anyone have any ideas how to make this work so that it
a. returns results
b. does not take half an hour to run?
SELECT *
FROM OpenQuery(ServiceNowUAT,
'SELECT name FROM DATABASE.[SCHEMA].[TableName]
WHERE CAST(sys_updated_on as TIMESTAMP) BETWEEN DATEADD(DAY, -2, now()) and now()'
)
Spark Interactive SQL Reference.pdf
I have a problem saving 'contable dates' because every month on this way has 30 days each. I need to save a element (2014-02-30) using a type date-like (not a varchar/text/blob/etc) to save this because in this project we need that. Is it possible?
Saving such a DATE "value" in a DATE or DATETIME column is possible using the sql_mode ALLOW_INVALID_DATES and no strict mode:
ALLOW_INVALID_DATES
Do not perform full checking of dates. Check only that the month is in
the range from 1 to 12 and the day is in the range from 1 to 31. This
is very convenient for Web applications where you obtain year, month,
and day in three different fields and you want to store exactly what
the user inserted (without date validation). This mode applies to DATE
and DATETIME columns. It does not apply TIMESTAMP columns, which
always require a valid date.
So checking the date for an allowed contable date could be done with triggers, since there's no other check too. I assume that for this application the 31th of each month would be an invalid date.
Example:
CREATE TABLE example (
contable_date DATE NOT NULL
) ENGINE=INNODB;
-- set the sql_mode (here for the session)
SET ##SESSION.sql_mode = 'ALLOW_INVALID_DATES';
INSERT INTO example (contable_date) VALUES ("2014-02-30");
SELECT
DAY(contable_date) as cday,
MONTH(contable_date) as cmonth,
TIMESTAMPDIFF(DAY, contable_date, '2014-03-30') as cdiff
FROM
example;
Result:
cday cmonth cdiff
-------------------
30 2 28
Demo
Using MySQL Workbench I get with
SELECT contable_date FROM example
following result:
contable_date
-------------
2014-02-30
but this doesn't work at sqlfiddle.com.
I wouldn't recommend this though, especially because one's not able to use strict SQL mode. One should consider the effect on the date and time functions too.
I have the following mysql table:
tasks:
=====================
tid
status
desc
duedate
And i have the following records in that table:
records
===========================
1
active
Test description
08/15/2014
2
active
Another description
08/31/204
I am trying to get the days that there is a task for, in that particular month. I have the following query but when i run it it gets both records but "day" is null on both of them for some reason. Can someone please help me with this.
MYSQL QUERY
====================
SELECT DATE_FORMAT(due_date,'%d') AS day FROM tasks WHERE due_date BETWEEN '08/01/2014' AND '08/31/2014'
Try:
SELECT DAY(due_date) AS day
FROM tasks
WHERE due_date >= '2014-08'
AND due_date < '2014-09';
DAY() is a better function for what you want and I prefer using >= and < than BETWEEN for date comparisons, as it allows you to specify precise ranges more easily. Here, for example, you don't need to know the number of days in the month.
I have also used the default date format, which is preferable. If you need the, in my opinion, cray American date format, use DATE_FORMAT() in your SELECT.
This will only work with DATE, DATETIME and TIMESTAMP columns, which is how your due_date should be stored, preferably DATE.
UPDATE
To convert the VARCHAR column to DATE run:
UPDATE tasks SET due_date=STR_TO_DATE(due_date,'%m/%d/%Y')
Then change the type. Also remember to change your INSERT statements to use the default format.
You've got to convert those "date" strings to proper date values with STR_TO_DATE:
SELECT
DAY(STR_TO_DATE(due_date,'%m/%d/%Y')) AS day
FROM tasks
WHERE
STR_TO_DATE(due_date, '%m/%d/%Y')
BETWEEN STR_TO_DATE('08/01/2014' '%m/%d/%Y')
AND STR_TO_DATE('08/31/2014', '%m/%d/%Y')
else you're comparing strings instead.
Note:
It would be better to use a proper DATE or DATETIME column instead.
With the current VARCHAR format MySQL is unable to use indexes. That's very bad for performance.
You can convert your data by adding another column to your table:
ALTER TABLE tasks
ADD COLUMN new_due_date DATE;
Then you use an UPDATE statement to fill this new column
UPDATE tasks
SET new_due_date = STR_TO_DATE(due_date, '%m/%d/%Y');
If you don't need your old column anymore then you can delete this column and modify the new column to have the name of the old one. Then you will have your table with all your data in a DATE column.