I've got a really random bug in my code somewhere but can't figure it out. I'm inserting data into a MySQL Database based the current date and use the following statement;
INSERT INTO table VALUES (NULL,495297,str_to_date('19/01/2013 10:55:25','%d/%m/%y'),'English - UK',1,Str_to_date('17/01/2013','%d/%m/%y'),str_to_date('18/01/2013','%d/%m/%y'))
none of the dates work as the 19/01/2013 for some reason it becomes 19/01/2020, the next become 17/01/2020 and 18/01/2020! The latter two dates are listed as Date in data Type whereas the first date is DateTime so I don't know why this bizarre problem is happening.
Any help would be greatly appreciated.
Thanks
Maudise
Use a capital %Y as identifier for the year:
INSERT INTO table VALUES (NULL,495297,str_to_date('19/01/2013 10:55:25','%d/%m/%Y'),'English - UK',1,Str_to_date('17/01/2013','%d/%m/%Y'),str_to_date('18/01/2013','%d/%m/%Y'))
The lower case %y specifies a year given by two digits (see MySQL documentation here and here). The uper-case %Y, however, is the four-digit version.
Related
I'm trying to add a computed column into an existing database table that will calculate age e.g. "23" from field "DOB" stored as YEAR (4) e.g. 1988.
Using the below, I get a
1582 error - an incorrect parameter count in the call to native function 'DATEDIFF'.
ALTER TABLE names ADD Age INT GENERATED ALWAYS AS (DATEDIFF('year', DOB, CURDATE))
Many examples I've found online have similar layout in terms of parameter count (some use GETDATE or NOW instead of CURDATE).
Anyone have any idea what I'm doing wrong?
Thanks to RigsFolly & Barmar for the feedback. I made a fresh cup of tea and looked at it again.
With DATEDIFF, it seems the MYSQL Server syntax can use the datepart and then use two dates to calculate (including GETDATE). I'm using MySQL through myphp admin which doesnt seem to like the datepart.
I dropped the datepart and changed the date format of the stored DOB field from "YEAR" to "DATE". As a result I no longer received the parameter error and by default the output of the DATEDIFF was returned in days. I then divided the output by 365 to get years.
My original order was wrong too which gave me the DATEDIFF output as a negative value, so changed the order and it works a charm. Thank you.
ALTER TABLE names ADD Age INT GENERATED ALWAYS AS (DATEDIFF(CURDATE(), DOB) / 365);
I have a large dataset with employees' time entries. The current date format is MM/dd/yyyy. However, I need to convert all the dates into yyyy-MM-dd format.
I have tried the following:
Update human_resources.timekeeping
Set Actual_Date = str_to_date(Actual_Date,'%d-%m-%Y');
Got the errror messsage Error Code: 1411. Incorrect datetime value: '' for function str_to_date.
My SQL version is 5.7.18-log.
I tried to view SQL mode using SELECT ##sql_mode; and I got NO ENGINE SUBSTITUTION.
I have tried to retrieve the value like shown below and it was working fine.
Converting varchar mm/dd/yy to date format yyyy-mm-dd
However, updating the data would not work. I need to update the actual records, not insert new records.
Hope someone can help me regarding this. Thank you in advance!
EDIT: The data type for Actual_Date is VARCHAR.
Apologies if my explanation may be a bit confusing. But I am using this data set to display and filter time entries in a gridview. When I am filtering dates, say for example (01/15/2022-01/25/2022), data from 2021 is also being displayed. When I tried to manually change the format of some of my data in sql to yyyy-MM-dd, my code seemed to be working fine. The problem is there are a lot of data in this table, which is why manually updating the format is impossible. What is the first thing that I need to do? I'm sorry this is all still a bit confusing for me.
My apologies if you have already taken the following things into consideration but I thought them worth mentioning.
Given that you say this is a "large dataset" I assume this is a table that is currently in use. Does the existing application rely on the Actual_Date being in that string format? Does it rely on a fixed number of columns in the table? Some poorly written applications can be very brittle when it comes to changing underlying data structure.
You may want to consider creating a copy of the current table, modifying the structure of the copy, and replacing the original with a view with the same columns and formats as the original. This way you get improved data but reduce risk to existing application.
In the title and first line of your question you state that the current format is MM/dd/yyyy
Update human_resources.timekeeping Set Actual_Date = str_to_date(Actual_Date,'%m/%d/%Y');
Your separator is / not -
%d-%m-%Y >> %d/%m/%Y
Using MySQL 5.7, it's pretended a conversion from strings representing four-digit year followed by two-digit month with no any other character in the middle.
For example, running the following statement
SELECT str_to_date('202105','%Y%m');
What is returned: 160101 being January 1601 instead of May 2021. The required arguments seem correct considering the function in cause.
I was not able to reproduce your exact result on DBFiddle, but I did see it creates a null instead of the expected date. What you can do is append a '01' to get a better date literal:
SELECT str_to_date(concat('202105', '01'),'%Y%m%d');
See it here:
https://www.db-fiddle.com/f/sbArVsLF6BVDGSrYdy8geP/0
You need to specify the day as well, otherwise it won't work in MySQL 8.0+.
You need all parts of the date i.e. year, month and day must be specified for the function to operate properly.
# Incorrect Code:
SELECT str_to_date('202105','%Y%m');
# Correct Code:
SELECT str_to_date('20210501','%Y%m%d');
Outputs
2020-05-01
Ref: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date
I was trying to enter date into MySQL and was trying to figure out the right syntax through trial and error. In one such command, MySQL accepted the value, however when I displayed the values it showed all zeroes.
can anyone help me understand why?
If you are not passing Date in default format then you need to intimate system that I am passing this string as date by mentioning format of date as describe below.
INSERT INTO test VALUES STR_TO_DATE('03-12-2016','%d-%m-%Y');
Hopefully this will help.
you must ensure that the value passed to mysql is in year-month-day sequence ("YYYY-MM-DD")
INSERT INTO test VALUES ('2016-03-02');
note:- please use another keyword instead inbuilt keyword.date is inbuilt mysql keyword.
Read here
Try this and also check your date format(data type) set in the database
INSERT INTO test VALUES ('2-03-2016')
Trial and error? This isn't some uncharted scientific territory where you need to strap on some goggles and use a bunsen burner. There's a whole chapter in the manual devoted to it.
Dates in MySQL should be in ISO-8601 format, that is YYYY-MM-DD or YYYY-MM-DD HH:MM:SS in 24-hour notation.
Please, before wasting tons of your time on pointless experimentation:
READ THE MANUAL
I've got the following part of a query:
STR_TO_DATE(CONCAT(g.Year,',',g.Month,',',g.Day),'%Y,%m,%d') AS HelpDate
g is a table of course. I know the values of g.Year, g.Month and g.Day all work fine because if I take out the STR_TO_DATE call and just use:
CONCAT(g.Year,',',g.Month,',',g.Day) AS HelpDate
it works fine. For some reason mysql breaks with the addition of the STR_TO_DATE. As far as I can tell this should work. I even tried hard coding the date just to force it to make sure I wasn't crazy:
STR_TO_DATE('2011,9,2','%Y,%m,%d') AS HelpDate
That didn't work either
Edit:
Ok. So the reason STR_TO_DATE wouldn't work at all for me was because the server was running MySQL 4.0. I've since moved the database to a MySQL5 server. Now, here's my problem. STR_TO_DATE only works as long as I'm not trying to concatenate table references. For example, both of these work:
STR_TO_DATE('2011,9,2','%Y,%m,%d') AS HelpDate
STR_TO_DATE(CONCAT('2011',',','9',',','2'),'%Y,%m,%d') AS HelpDate
However, this still does not:
STR_TO_DATE(CONCAT(g.Year,',',g.Month,',',g.Day),'%Y,%m,%d') AS HelpDate
I get this error:
#1305 - FUNCTION db380975735.STR_TO_DATE does not exist
db38097573 is the name of the database
You're looking for the format '%Y,%c,%e'. %m and %d matches the month/day when it begins with a zero (it would match 2011,09,02).
By the way, what's the point to store the year, month and day separately? Store it in a DATE type, and use the functions YEAR(), DAY() and MONTH() if necessary.
Try using:
STR_TO_DATE('2011,9,2','%Y,%c,%e') AS HelpDate
%m and %d assume formatting with leading zeroes, i.e. numbers that are always two digits long. Thus STR_TO_DATE('2011,09,02','%Y,%m,%d') AS HelpDate would probably work. %c and %e refer to the variable-length numeric month and day respectively.