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

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.

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');

Use date in mysql database instead ID

How is smart to use date as primary value instead ID?
Where I can get better search mysql database performace:
to use timestamp: 1394319600
or to format date and use it as: 09032014
09032014 = 1394319600 = 9.Mart 2014
You likely should not be using a datetime type of data field as a primary key to begin with. I would suggest using autoincrementing integer field to guarantee uniqueness.
Now with regards to the datetime/timestamp field itself it is almost always better to use a native datetime or timestamp data type for these columns rather than string representations such a unix timestamps or other formatted date strings.
Why? Because when people put in timestamp data into their database base, they typically get to the point of wanting to run queries against that data. If you store your data in a non-native datetime format, you will typically need to convert it to such a format before you can use it in typical date/time functions that would be used in this sort of query. This usually means you lose the ability to leverage any index on the field for the query.
For example, say you wanted to run a query to see all records for the current day. With unix timestamp field that query may look like:
SELECT * FROM table
WHERE FROM_UNIX_TIMESTAMP(timestamp_field)
BETWEEN CONCAT(CURRENT_DATE(), ' 00:00:00') AND CONCAT(CURRENT_DATE(), ' 23:59:59')
whereas with a datetime/timestamp filed it would look like:
SELECT * FROM table
WHERE timestamp_field
BETWEEN CONCAT(CURRENT_DATE(), ' 00:00:00') AND CONCAT(CURRENT_DATE(), ' 23:59:59')
Here the simple requirement to use FROM_UNIX_TIMESTAMP() on the left hand side of the WHERE condition in the first query prevents use of an index since FROM_UNIX_TIMESTAMP(timestamp_field) does not exist in memory like timestamp_field would if properly indexed. This means you now need to do a full table scan to execute that query. If you have a large table, this could be very problematic.

What datatype to use for ISO 8601 dates?

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.

Which is the correct datatype to store time in SQL?

Here's my table right now (using mysql):
SQL Table: koko_table
name varchar(140)
status varchar(140)
time TIMESTAMP
My issue basically is , I have a form (using php) which user uses to store data only in the status column, my time column captures the time as a permanent data when the user stores in the status column. I think I have not been using accurate DATATYPE for time column, because everytime I visit my database, the time column has different values.
What can be the correct datatype to store time of status input by the user as un-changeable data.
The best data type would be DATETIME.
You can use either TIMESTAMP or DATETIME in MySQL to store date and time.
There are differences though:
TIMESTAMP uses 4 bytes, DATETIME 8 bytes.
Timestamps can be between 1970 and 2038, while datetimes can be between 1000-01-01 00:00:00 and 9999-12-32 23:59:59.
TIMESTAMP values are converted from the current time zone to UTC for storage, and converted back from UTC to the (server's) current time zone for retrieval. This does not happen for DATETIME values where no timezone is implied.
Datetime fields have to be declared in INSERT operations while timestamp fields have the special feature that the first timestamp of a table is (by default) automatically inserted or updated at every INSERT or UPDATE operation with the current timestamp. (That's probably what you are seeing in your scenario.) You can change this behaviour, so only Inserts or only Updates set the timestamp to current timestamp. See MySQL docs: Timestamp properties
To have for example the timestamp automatically stored at Inserts but not changed during Updates, you could set:
ALTER TABLE TableName
CHANGE TimeStampName TimeStampName TIMESTAMP DEFAULT CURRENT_TIMESTAMP ;
I'd prefered DATETIME (the long number) or store it as a formated string like 2011/10/18 13:50. The first one is better in performance and the secound one is easyer to edit on phpMyAdmin or something like that.

Default date in mysql

i am facing issue while creating table where in that one column which is set to default value of current date........i would stress this point "that i need only date not time along with that"....i would be really thankfull to those who tries to help me....
Instead of trying to get MySQL to insert the current date for you when a row is created automatically, you can set it to NOW() in the INSERT query. Or, in the code for your software, you can set the date to the current date in the INSERT query.
MySQL does not support a default of the current date for DATE or DATETIME.
Check out this set yuor field to a 'Date' not 'DateTime'
http://dev.mysql.com/doc/refman/5.1/en/datetime.html
The DATE type is used when you need
only a date value, without a time
part. MySQL retrieves and displays
DATE values in 'YYYY-MM-DD' format.
The supported range is '1000-01-01' to
'9999-12-31'.