MySQL incorrect timestamp value - mysql

I'm trying to insert datetime value '1970-01-01 00:00:01' in timestamp column but MySQL returned an error "Incorrect datetime value: '1970-01-01 00:00:01' for column 'timestamp'"
CREATE TABLE TST_TABLE
(
tst_column timestamp NULL
)
INSERT INTO TST_TABLE(tst_column) VALUES('1970-01-01 00:00:01');
I'm confused because MySQL documentation claims that lowest valid value for timestamp is '1970-01-01 00:00:01'. What's wrong and what is real lowest timestamp value?
Thanks.

This is a timezone issue. Set the timezone to UTC before the insert, for example :
SET time_zone='+00:00';
INSERT INTO TST_TABLE(tst_column) VALUES('1970-01-01 00:00:01');
An other option is to convert you timestamp to the UTC timezone using CONVERT_TZ. For exemple, if your timezone is Europe/Paris :
INSERT INTO TST_TABLE(tst_column) VALUES(CONVERT_TZ('1970-01-01 00:00:01', 'Europe/Paris', 'UTC'));

Related

Datetime 0000-00-00 00:00:00 using CURRENT_TIMESTAMP

I'm running xampp 3.3.0, mysql 8.0. In my database, there is a column datetime with type "datetime". I'm trying to insert data in it with CURRENT_TIMESTAP.
Example of query:
INSERT INTO `statistics` (`chat_id`, `user_id`, `message_id`, `datetime`, `ai_unique`) VALUES ('988', '767', '98765', 'CURRENT_TIMESTAMP', NULL);
It inserts, but I'm getting
Warning: #1265 Data truncated for column 'datetime' at row 1
And instead of current timestamp, it inserts 0000-00-00 00:00:00
Some time ago it worked just fine, but now it's inserting 0000-00-00 00:00:00. If I manually write a date here like 2023-01-11 22:52:01 it works just fine
'CURRENT_TIMESTAMP' (with single-quotes) is a string. It doesn't have a datetime value. MySQL makes an effort to convert the string to a datetime, just as it would convert the string '2023-01-24 13:09:00' to a datetime. But the string you used cannot be converted in this way.
CURRENT_TIMESTAMP (a pseudoclumn with no single-quotes) has a datetime value.
CURRENT_TIMESTAMP() (a function) also returns a datetime value.

What is the minimum date time value that can be entered in MySQL column of timestamp datatype?

I'm trying to insert a date time value e.g. 1970-01-01 00:00:01 in a column of timestamp datatype in MySQL table but getting following error:
#1292 - Incorrect datetime value: '1970-01-01 00:00:01' for column 'order_date' at row 1
But according to MySQL docs - The TIMESTAMP value has a range from '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
So if the range value starts from 1970-01-01 00:00:01 UTC then why this value can't be inserted in the table? Is there something to do with UTC? What will be the minimum date time value for timestamp that can be inserted without any issue?
Timestamp columns store a utc value and start at 1970-01-01 00:00:01, but whenever you read or write them, they convert to/from your session timezone. This makes them something of a nightmare to work with if you are actually intending to only use UTC.
Just use a datetime type if you ever are trying to set particular times that come from your client. If you must use timestamp and want to set a particular UTC time, first do:
set session time_zone='+00:00';
But note that any client that doesn't do that may see a different time. Even if you set your server timezone to UTC, so that sessions default to UTC, some client libraries "helpfully" set the session timezone when they connect.

Invalid datetime format: 1292 Incorrect datetime value: '2047-10-23 23:29:24' for column 'date'

When I insert the date as '2019-10-10 00:00:00', my operation can be completed.
But when I inserted the date as '2047-10-23 23:29:24', my mysql reported an error.
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2047-10-23 23:29:24' for column 'date' at row 1
mysql version:5.7.21
You need to check your field's datatype. If its timestamp then update that to be a datetime
From MySql Reference:
The DATE, DATETIME, and TIMESTAMP Types
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
The DATETIME type is used for values that contain both date and time
parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD
hh:mm:ss' format. The supported range is '1000-01-01 00:00:00' to
'9999-12-31 23:59:59'.
The TIMESTAMP data type is used for values that contain both date and
time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to
'2038-01-19 03:14:07' UTC.

MySQL - How to select rows where datetime field is not equal to 0000-00-00 00:00:00?

Here is my table "tb_posts":
I want to select only those rows where datetime field i.e. post_date_published is not equal to 0000-00-00 00:00:00. I am using following query but it doesn't work:
SELECT * FROM `tb_posts` WHERE `post_date_published` IS NOT NULL
I am getting the same output as shown in the above picture.
Why IS NOT NULL is not working?
As per the MYSQL documentation it saves invalid dates as '0000-00-00 00:00:00'. It will not be considered as NULL.
Try comparing with the date '0000-00-00 00:00:00':
SELECT * FROM tb_posts where post_date_published != '0000-00-00 00:00:00'
A method I use with this sort of thing is
SELECT `columns` FROM `tb_posts` WHERE UNIX_TIMESTAMP(`post_date_published`) > 0
From the MySQL Documentation:
The valid range of argument values is the same as for the TIMESTAMP
data type: '1970-01-01 00:00:01.000000' UTC to '2038-01-19
03:14:07.999999' UTC. If you pass an out-of-range date to
UNIX_TIMESTAMP(), it returns 0.
The UNIX_TIMESTAMP function forces the result to be an integer so it's much easier to work with in these quick comparisons. It is also vital for working with MySQL 5.7 where "empty" (ie zero value) date/time columns are not allowed.
(I had a lot of grief trying to convert various date columns to NULL because MySQL 5.7+ didn't recognise 0000-00-00 00:00:00 as a valid comparison -- so I converted it to a unix timestamp so as to compare the timestamp rather than the actual [invalid] date.)

I am coverting unix timestamp to Datetime format in mysql .But it is showing NULL as an output

select FROM_UNIXTIME(32154654321);
Output:Null
Expected Result: Tuesday, December 9, 2988 1:55:21 PM GMT+05:30
This is why: https://dev.mysql.com/doc/refman/5.5/en/datetime.html
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
This is the Year-2038 problem. The maximum value of the TIMESTAMP datatype is 2038-01-19 03:14:07, so you can't get the expected result.
MySQL 5.5: The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
source: https://dev.mysql.com/doc/refman/5.5/en/datetime.html
MySQL 5.6+: [...] and the range for TIMESTAMP values is '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999'.
source: https://dev.mysql.com/doc/refman/5.7/en/datetime.html
You can find a very good answer on stackoverflow with some more details about this problem.
You can use PHP instead of MySQL to convert the timestamp (not stored in a TIMESTAMP column) to a readable datetime, with the following code:
$timestamp = 32154654321;
$format = 'Y-m-d H:i:s';
$date = new DateTime();
$date->setTimestamp($timestamp);
echo $date->format($format);
demo: http://ideone.com/DsYUOQ
You cannot input like 32154654321
Timestamps in mysql have a maximum value of 2147483647, equivalent
to 2038-01-19 05:14:07. This is due to the underlying 32-bit
limitation. Using the function on a timestamp beyond this will result
in NULL being returned. Use DATETIME as a storage type if you require
dates beyond this.