Mysql wrong datetime format - mysql

I am inserting date from Android app in this format 2013-11-30T00:00:00.000+01:00 but when I see the stored data, it shows like this: 2013-11-30 00:00:00.
I tried different structures - timestamp and datetime, it's always the same. Why is it changing, or what am I doing wrong? Thanks

If you check out the reference guide for The DATE, DATETIME, and TIMESTAMP Types, you'll see both DATETIME and TIMESTAMP store the data in the format YYYY-MM-DD HH:MM:SS.
If you want to store it in the original format then you can use CHAR instead.

Related

MySQL Not Accepting Date Format For Some Dates

FYI - I'm still a database newbie. That said, One of my MySQL tables has a column called "FirstDetect"
This column houses dates and times in the following format "2017-31-08 14:30:05". I'm importing a CSV file and everything is working just fine. However, some of the dates are not being accepted and the date value is reverted to 0000-00-00 00:00:00.
I assume it has something to do with the field settings. I can manually type this date (2017-05-09 00:17:42) into a field under the FirstDetect column, save the results and that date is written to that field as expected.
However, when I try to manually type this date (2017-31-08 14:30:05) into a field under the FirstDetect column, save the results, it reverts back to 0000-00-00 00:00:00. Anyone know what's going on here?
I'm using MySQL version 5.5.57
Default format that datetime field uses is YYYY-MM-DD HH:MM:SS. If you're trying to enter your dates in YYYY-DD-MM format, it's not going to work and it's probably going to be reverted as you described.
You can learn more from the link below.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

How to convert datetime string of different formats in a valid datetime in SQL 2008

I have a table in SQL database in which there is an NVARCHAR(MAX) column. In it, the data is stored as 'Datetime1&Datetime2&Datetime3&Datetime4...'(& separated dates. I know this is not correct way to store data but I have to support an older system).
Now the issue is that this table is used in multiple sites and hence the data coming in this column could come in different formats. Means, for one site, the datetime could be in the format '22-06-2015 13:00:00' and for some other site, the datetime could be in the format '2015-06-22 13:00:00' or '2015-22-06 13:00:00'.
I have to break the data from this column and store individually each value as a datetime in a new row in another table. This has to be datetime only as I need to do some date operations like comparison, Datediff, Dateadd etc.
So, in my Stored Procedure, if I select value as convert(datetime,[date],121), then it gives error if the string is in any other format than 121(yyyy-mm-dd hh:mm:ss.mmm).
So how can I address this problem that this datetime string could be converted to actual datetime without any issue of a particular format or system format?

SQL Date format in schema definition

For "date1" attribute of my table I am trying to insert date of the format
YEAR-MONTH-DAY HOUR:MIN:SEC
Do I have ot define this format in the schema?
All I have done is
date1 DATE NULL
I know I can use DATE_FORMAT(), but I think only for retrieving/querying purposes.
What about recording data in the table?
if you are trying to insert a date with the time you need to instantiate it with datetime
date1 DATETIME NULL
SEE DOCS
alternatively you can store it as a TIMESTAMP. both store in that format
Your schema is fine (though you'll need DATETIME if wanting to preserve the time component), there aren't formatting options for how a DATETIME type is stored.
As long as the data you're inserting has a valid datetime format (the one you posted is fine) then you don't need to do anything else. Some non-standard formats will require a fix prior to insert.
The DATE_FORMAT() function is for displaying a DATE/DATETIME in a chosen format, but how the data is stored is defined solely by the data type.

Retrieve data with different datatype

I am storing data in my table with char datatype but there are date values also.
The table has only 3 columns
Name Field Content
The problem comes when i try to retrive dates from the data and have them in ascending or descending order since mysql treats them as strings instead of dates the output isnt quite correct.
Is there anyway i could retrieve the dates telling mysql to treat them as dates and not strings?
thats probably because it is not a datetime datatype in the database.. its a string.. use the STR_TO_DATE() function to convert it to a date.

Change datetime format within CREATE TABLE

I hope you might be able to give some advice. I'm trying to create a table within SQL Server 2008 and I need to update my DateTime column to a different format.
CREATE TABLE #History (Tag nvarchar(512), User nvarchar(40), DateTime datetime, Quality int)
However I need to make the DateTime column return a DateTime value in the following format:
yyyy-MM-dd THH:mm:ss
Thanks in advance for any advice you can give.
There's no dateformat there's only the binary data!
Your date data is persisted as bunch of 0 and 1 and got no format at all.
when you retrieve that data, let's say in a select the query analyzer ill show you a representation of that data, by default a ISO formated datetime.
If anything consuming that result set needs a specif date format you can use cast/convert or set dateformat (and language) to achieve that.
See Damien comment for a good example.