MySQL create Datetime field from Timestamp field - mysql

I have a simple problem for which I found a solution in early testing but cannot replicate now that I need to do it for real.
We are moving our news system from CuteNews to our self-built MySQL system. All the news has been imported into the database but the date field is Timestamp. I have created a new field (created_date) and want to populate this from the Timestamp in the date field.
As I said, I did do this previously in an early trial run and was convinced that the query I used, via PhpMyAdmin was along the lines of UPDATE News set created_date=UNIX_TIMESTAMP(date). This is probably slightly wrong as I am at work and posting from my phone but it was along those lines.
No errors were returned but all 'created_date' fields were populated as 0000-00-00 00:00 not with the date taken from the Timestamp in the date field.
I know it is simple and know the answer will be obvious when I see it but any pointers would be gratefully appreciated!
Steve.
EDIT: reading back through I realised a bit of what I posted may be misleading. In my original trial run using the update query put the correct DateTime in the field based on the corresponding Timestamp field. It is only this time that it shows 0000-00-00 00:00.
Ps. Thanks for the format tidy-up. It's a bit awkward on a phone!

I knew I was nearly there!! It was not UNIX_TIMESTAMP but FROM_UNIXTIME I was after.
UPDATE news SET created_date = FROM_UNIXTIME(date)
Thanks for the help.
Steve

Related

A field that stores the time the row was inserted and doesn't change it on update

According to what I've read in some posts such as the answer for this question
passing null value in a timestamp field will store the date when the row was created, but will update it when row is edited.
I want to do the same thing but without changing the date, and having that time stored as UTC, how can I do that?
The best approach is to create a trigger to apply the change, or do it in your application code.
As a note, TIMESTAMP fields are really awful and shouldn't be used as they are subject to the 2038 overflow issue while DATETIME is not. A DATETIME can go thousands of years into the past and into the future, which is hopefully enough.

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

MySQL DATE_ADD INTERVAL 1 SECOND not working

I'm facing currently very, very strange behaviour.
I have an enterprise application, running on JBoss, the ORM is as you might expect hibernate and I'm doing some bulk operations.
I have a table and in that table there are all kind of fields and among the others there is a field of type DATETIME. In this field I'm saving user info, I have another field of the same type, configured with an trigger, so every time when the row is being updated the second DATETIME field is also updated.
I have a case where the first field should be increased with one second, so I'm doing bulk copy something like this:
UPDATE <TABLE_NAME> SET customerDATE = DATE_ADD(customerDATE, INTERVAL 1 SECOND)
My problem is that the query does not always work as expected.
What I see is that the second DATETIME field(the onne with the trigger) is being updated, together with some other fields, but the auto-increment with one second does not alway works(sometimes is also increased, sometimes not).
I tried searching for some known issues, but without success.
If anybody knows some problem in this direction I would really appreciate any help!
I'm testing now if there will be problems with more than an extra second.
Thanks in advance!

CURTIME() SQL Issues

I have tried multiple ways of formatting/retrieving just the TIME portion of a DATETIME function without success. Everything I have read online says CURTIME() will return the current time. It does, but returns the date as well. I have no interest in the date, as the field I am filling is a time only field. Granted the field type is DATETIME.
When I run my code, I view my table through Access 2007. I see the date as well as the TIME portion. When I do the CURDATE() function for my Date field it shows just the DATE just fine.
I have extensively googled this issue of having the DATE portion appear, but every single seemingly 'fix' to the issue is the same thing over and over, ie. Casting/converting (CONVERT doesn't seem to be a function in the current MySQL ODBC version I am using). When attempting to be simple and UPDATE the time with a string, simply nothing shows up in the Field when I refresh the Access tables.
Anyone have any ideas? Google has failed me.
Figured it out... For what ever reason the only spot for formatting that would be accepted was in the format field in the Design View from the Access 2007 view of the tables. hh:mm:ss AM/PM was necessary in the format field, apparently this is the only way to format a CURTIME() or any DATETIME function from a C# into the SQL/database I was using at least.

compare timestamps in mysql

I save timestamps in my database in this format 2012-04-16 08:58:55. I read a timestamp of my database and then i want to use this timestamp in another query and ask from the database to return records where the timestamp is greater equal than this timestamp. I am using the ">=" but it is not working.
I am trying this one:
$query="SELECT DISTINCT timestamp,text FROM array WHERE id='$theID' AND timestamp>='$thisTimestamp'";
What exactly does "not working" mean? It's not clear whether you want to compare datetime stamps or simply the time porition.
For the former, check this thread Mysql Compare two datetime fields, for the latter, simply use the TIME() function in your query e.g. SELECT * FROM table WHERE TIME(datetime) >= '08:58:55';
Clarify your question if you are in search of something else.
EDIT: Have you not read my first link? That is exactly what you need given the problem you have provided so far. What results are you looking for? Give an example and then give an example of how your query is performing incorrectly. Without this information, no one will be able to give you complete help!