What datatype to use for ISO 8601 dates? - mysql

I am redesigning a database that currently imports ISO 8601 dates in the format of 2012-10-27T07:30:38+00:00 into a varchar field. The current database is hosted on a MySQL 5.5 server.
In searching through documentation and various SO posts, I have not found a definitive answer on what datatype I should use in MySQL for this purpose. The closest post is: MySQL insert to DATETIME: is it safe to use ISO::8601 format? where it provides a work around of sorts, however this is not a desirable option.
The MySQL documentation (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-types.html) does not say, and the only reference I can find on official documentation is located on page: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
which states "The possible values for the first and second arguments result in several possible format strings (for the specifiers used, see the table in the DATE_FORMAT() function description). ISO format refers to ISO 9075, not ISO 8601."
Now the PostgreSQL documentation specifically mentions ISO8601 ( http://www.postgresql.org/docs/9.2/static/datetime-keywords.html and http://www.postgresql.org/docs/9.2/static/datatype-datetime.html ) which leads me to my question:
Does MySQL correctly support ISO 8601, or should I consider a database with native support?
--Edit--
Attempting to insert the example time stamp above into a datetime column gives the following error:
10:55:55 insert into test(date1) values('2012-10-27T07:30:38+00:00') Error Code: 1292. Incorrect datetime value: '2012-10-27T07:30:38+00:00' for column 'date1' at row 1 0.047 sec

Do not store date or timestamp values in a varchar column. You have no way of ensuring that a correct value is stored (no one prevents you from storing 2012-02-31 28:99:70 in there.
If you don't need the time part, use a date datatype (available in MySQL and PostgreSQL) if you do need the time use a timestamp (or datetime in MySQL) data type.
The formatting of the values should be done in your frontend or if you absolutely have to do it in SQL using e.g. to_char() (or the equivalent in MySQL) when retrieving the values.
Again: never store dates or timestamps in a varchar (just as you should never stored real numbers in a varchar column).
Here is the MySQL overview for date/time datatypes: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-types.html
Here is the PostgreSQL overview for date7time datatypes: http://www.postgresql.org/docs/current/static/datatype-datetime.html
Edit
If you are concerned about the literal format. Both DBMS support the standard ANSI Date and Timestamp literals:
insert into some_table
(ts_column, date_column)
values
(timestamp '2012-10-27T07:30:38+00:00', DATE '2012-12-30');
SQLFiddle for PostgreSQL: http://sqlfiddle.com/#!12/cdd39/1
Note the keywords timestamp and date in front of the character literal.
Edit 2
It seems MySQL cannot insert such a value, although it can use that literal in a SELECT statement:

Postgresql can handle that format:
without
select timestamp '2012-10-27T07:30:38+00:00';
timestamp
---------------------
2012-10-27 07:30:38
or with time zone:
select timestamptz '2012-10-27T07:30:38+00:00';
timestamptz
------------------------
2012-10-27 05:30:38-02
Whereas MySQL seems to not care much about the time zone part:
create table t (ts timestamp);
insert into t (ts) values
('2012-10-27T07:30:38+03:00'),
('2012-10-27T07:30:38-02:00');
select * from t;
+---------------------+
| ts |
+---------------------+
| 2012-10-27 07:30:38 |
| 2012-10-27 07:30:38 |
+---------------------+

The ISO 8601 date format, "YYYY-MM-DD", is what MySQL emits internally when displaying date values and it can import them just the same.
These are preferable to "American style" dates like "MM/DD/YY" where there's too much ambiguity to be automatically resolved.
ISO 9075 appears to refer to the entirety of the SQL standard, not a specific date format, though the standard itself does have standard formatting for dates and times.

Just another advantage of using datetime or timestamp for storing.
In phpMyAdmin or phpPgAdmin the value is displayed as a string e.g. 2015-01-22 11:13:42. So it's much easier to find a date as if it's stored as a varchar or int.
And I think it's important what timezone the server has.
I've see that a timestamp in MySql doesn't display microseconds.
I know MySQL use UTC-Time for timestamp and datetime-fields.
So provide the values as a UTC-timestamp.

Related

How to insert date in a new table in YYYY-MM-DD' format?

CREATE TABLE ORDERS (
ORD_NUM NUMERIC(6,0) NOT NULL PRIMARY KEY,
ORD_AMOUNT NUMERIC(12,2) NOT NULL,
ORD_DATE DATE NOT NULL,
INSERT INTO ORDERS VALUES('200100', '1000.00', '08/01/2008');
INSERT INTO ORDERS VALUES('200110', '3000.00', '04/15/2008');
INSERT INTO ORDERS VALUES('200107', '4500.00', '08/30/2008');
Since I have large number of rows with date in the above format, how can I convert the into yyyy-mm-dd format?
The below solution does not work as the values are not inserted into the ORDERS table.
So, nothing to update.
UPDATE ORDERS
SET ORD_DATE = DATE_FORMAT(ORD_DATE, '%Y-%m-%d');
Executing the code gives error, which I learned to be due to the date format, which MySQL does not allow.
The question misunderstands how dates work in SQL. The Date type does not have ANY human-readable format at all. The values in this column will be stored as binary data, which has significant benefits over string formats for memory/storage use, date math, and indexing.
Now we insert a value like '08/01/2008' into a date column. I will interpret this to mean August 1st based on the other values in the question (this isn't a universal or even majority interpretation!). This value provides the month first, then the day, then the year... but MySQL will not store it that way and does not preserve the original format.
Therefore it makes no sense at all to UPDATE the column to set a specific format. You can't do it, because dates are not stored in a way that preserves any write-able format.
What you can do is format the value at output time, as part of a SELECT query, to use whatever format you need. Additionally, you can use the Str_To_Date() function to control how string values will be interpreted when creating or comparing to native SQL dates.
One thing to keep in mind: thanks to cultural/internationalization issues, converting dates (and numbers!) to and from strings is much slower and more error-prone for a computer than you likely expect. It's something to avoid. Therefore, converting to the native date format early, and leaving it that way as long as possible, is usually the best option.
You just need to convert the string to a date in your insert statements:
INSERT INTO ORDERS VALUES('200100', '1000.00', TO_DATE('08/01/2008', 'mm/dd/yyyy');

Convert 2021-1-01 to 2021-01-01 in SQL (YYYY-M-DD TO YYYY-MM-DD)

As title.
There is a way to convert a date from YYYY-M-DD to YYYY-MM-DD?
In db I have 2021-1-01 but I need to get 2021-01-01.
TY
EDIT
I have a value 2021-1-01 and i need to insert it in a db like date 2021-01-01, maybe before I not was clear.
If you have '2021-1-01', then you do not have a date. You have a string. That is a problem with your data model. You should fix the data to store dates using appropriate types -- which are not strings.
MySQL is pretty smart about converting dates, so you can just use:
select date(string_as_date_col)
You can change the type of the column in the table using:
alter table t modify column string_as_date_column date
Here is a db<>fiddle.

How to specify the date format inside create table command

I have to create a table with the following columns
Emp_no [PK], Basic_pay_fixed_at, Date_of_birth
The date format for the Date_of_birth column is dd-mm-yyyy
As far as I know the default format for the date datatype in SQL is yyyy-mm-dd
How do I specify this alternate date format inside the create table command ? So, that when I insert value into the table I can insert in the dd-mm-yyyy format.
Thanks in advance!
MySQL (and most other RDBMS as far as I know) use YYYY-MM-DD as the standard string representation and cannot be changed. Any data inserted into such a field must be formatted in this manner, or converted to a true date value using functions such as STR_TO_DATE.
Similarly, selecting a values from a field actually returns datetime types in most client languages, which can then be formatted as needed; or other date functions can be used in the select expressions to yield the desired string.
Dates are stored internal so formatting is for input and output.
This will convert to the style you want for output:
select convert(varchar(10), Date_of_birth, 105) from Table

MySQL Timestamp return as datetime, not integer

I have MySQL table, where column _update is timestamp type. I have problem, and don't understand, why when I do select this column, is return in datetime format, not in integer?
In JavaScript Unix timestamp is returned as integer, in PHP time() is as integer, but in MySQL this is datetime? Why?
I know, that I can use bigint type, but I want add timestamp to row, when I insert data from phpmyadmin, and update timestamp, when data is update (this is possible to set only in timestamp type column).
I guess the doubt comes from the incorrect assumption that timestamp always refers to Unix time. However, it's a rather generic English word that's widely used in IT for fairly different time-related concepts.
In the case of MySQL TIMESTAMP column type, while it's true that it's stored internally as Unix timestamp:
TIMESTAMP values are stored as the number of seconds since the epoch
('1970-01-01 00:00:00' UTC).
... such detail is mostly irrelevant (as most internal representations are) because the interface is the same as any other date type: a string in a fixed human-readable format, e.g.:
'2038-01-19 03:14:07.999999'
And, as in any other date column type, you have the corresponding functions to convert from/to string whenever you have/need a different string format:
STR_TO_DATE()
DATE_FORMAT()

How to convert timestamp format to 2011-05-10T06:58:00?

I have a column in my drupal database called "field_concert_published_value" which is storing the values of the concert publishing (it is a cck field)
I have old records from previous system and there is stored this value as classical timestamp.
I know how to INSERT INTO ... however the format of the date time cck field is unusual. I mean e.g. 2011-05-10T06:58:00 . The letter T between the date and time, so I can not use FROM_UNIXTIME function to convert from my old records to this yyyy-mm-ddThh:ii:ss.
Any advice how to convert my old records in timestamp format to the new one which date time filed is using?
Thanks.
P.S. I need this to be done using SQL statement (MySql)
Have you tried STR_TO_DATE, FROM_UNIXTIMESTAMP and TIME_FORMAT?
SELECT DATE_FORMAT( DATE( FROM_UNIXTIME( `timestamp` ) ), '%Y-%m-%dT%H.%i.%s');
This page has a good explanation of the date tokens: MySQL Date Time Formatting.