Error in entering Date in My SQL - mysql

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

Related

How to convert date from MM/dd/yyyy to yyyy-MM-dd in MySQL? Error Code: 1411. Incorrect datetime value: '' for function str_to_date

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

MySQL syntax error for converting and formatting a date

I'm having trouble with a line of code in MySQL. I am attempting to write the following line of code into a select statement that I already have and that works. I have a comma both before and after the line to handle the field prior to this and after this. The support_due_date field is a date field.
ISNULL(DATE_FORMAT(support_due_date, '%m/%d/%Y'), '01/01/1900') as support_due_date2
I'm getting a syntax error. The support_due_date field has some null values and some date values. I am wanting to format the field to have a M/D/YYYY format and if the field is Null change it to 01/01/1900. What am I doing wrong? Any help would be appreciated. I'm using version 5.2.47 if that helps.
ISNULL() by itself merely evaluates to true or false based on the argument passed; I believe your syntax error is due to attempting to pass 2 comma-separated arguments to that function. What you're actually looking for is, I think, something more like if(isnull(support_due_date), '01/01/1900', date_format(support_due_date, '%m/%d/%Y')) as support_due_date2 .
The documentation for control flow statements (including if()) is here.
That said, eggyal makes a good point about magic values in their comment to your question - imho there is a time and a place for both approaches, just something to consider.

Format date in mysql query that uses cast

I've got this as the select part of my query:
SELECT cast(cast(exp_channel_titles.edit_date as char(14)) as datetime) AS Edit_Date
That takes data from a db in this format 20130501092128 and returns it in this format 2013-05-01 09:21:28
I can only assume it is some kind of magic as i don't fully understand how this works tbh.
But, i need to change the format of the date that it spits out to this format: %d/%m/%Y %k:%i:%s
I can honestly say i have no idea how to do this in that query, i've tried adding it as a param to datetime (is that even a mysql function?!?) but no joy and many other poor attempts that i wont go into.
If anyone can help, i'd be hugely grateful!
MySql automatically converts 20130501092128 to a date and time field, even if it is a VARCHAR or a INT, and you can just use this:
SELECT DATE_FORMAT(exp_channel_titles.edit_date, '%d/%m/%Y %k:%i:%s')
Please see fiddle here.
You can change output format using DATE_FORMAT() function from MySQL. Here is the documentation post about it.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
You can change the output format into whatever format you want, but if you recieve that data into an application, modifies it and return that data to server (editing a row for example). Remember to reformat it into a valid date for MySQL.
If you dont know how to do it, just have to do this into your query:
SELECT DATE_FORMAT(cast(cast(exp_channel_titles.edit_date as char(14))
as datetime), '%e/%m/%Y %k:%i:%s') AS Edit_Date

Weird Date MySQL INSERT statement (vb.net app but MYSQL command)

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.

SQL Server 2008 VarChar To DateTime

I have a table where unfortunately a number of dates are stored as strings.
I have a number of reports that cast them to datetimes and filter by them. This was working fine until today when all of a sudden i'm getting this error
"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."
The dates are all stored in the format of "yyyy-mm-dd" and are all valid.
If I run the following SQL statement
SELECT CAST('2010-06-02' AS DateTime)
I would expect to get "2010-06-02" however as of today I'm getting "2010-02-06" something has changed with the way SQL formats dates. I've had a look in regional settings on the server and it all looks to be correct.
What else could be causing this?
Try setting the format explicitly
select convert(datetime, '2010-06-02',101)
An unambiguous way of getting this conversion is to do the following:
SELECT CAST(replace('2010-06-02', '-', '') AS DateTime)
And that will always be interpreted as YYYYMMDD, ignoring the set dateformat ydm declaration or any cultural settings that the database has.
Q1: What else could be causing this?
The local, You probably are under the local101(US) and put data from 103 (British/French)
Like barry sad use convert