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.
Related
I am using created_at TIMESTAMP in my Cassandra schema and have the following code in my NodeJS app:
INSERT INTO books (id, user_id, created_at)
VALUES (uuid(), 'userOne', toTimestamp(now()));
I like to know, if I am using created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP in my mysql app and DateTime.now().millisecondsSinceEpoch in my Flutter application, will these TIMESTAMP values all match together? If not, what can I use for all of them?
In summary, if I use:
#MySQL Schema:
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
#Cassandra Schema and command:
//at the Schema
created_at TIMESTAMP
//inside the NodeJS code
INSERT INTO table_name (created_at)
VALUES (toTimestamp(now()));
#Flutter code:
String timestamp = DateTime.now().millisecondsSinceEpoch.toString();
They all should be same and compatible?
The CQL timestamp data type is a 64-bit signed integer that represents the number of milliseconds since Unix epoch. On the other hand, the CQL timeuuid is a version 1 UUID, i.e. a UUID with a timestamp included.
The CQL now() returns the system time in timeuuid format and is equivalent to the currentTimeUuid() function. When now() is nested in toTimestamp(), it returns the system time in timestamp format.
The CQL timestamp type supports BOTH integers and strings.
MySQL converts timestamp values into UTC. When setting CQL timestamp columns using MySQL timestamp values as string, it must conform to ISO-8601. The valid formats are:
yyyy-mm-dd (date only)
yyyy-mm-dd hh:MM:ss (date with time)
yyyy-mm-dd hh:MM:ss.fff (with time in millisecond precision)
yyyy-mm-dd hh:MM:ss.fff [+/-]NNNN (with RFC 822 4-digit optional timezone, e.g. +0000 for GMT or +1000 for Melbourne)
The Flutter DateTime.now().millisecondsSinceEpoch() returns an integer so it is also compatible with CQL timestamp type. Cheers!
insert into test_date
select
shopify_update
from `vw_shopify_json_price`
where cache_id=669
Error Code: 1292
Truncated incorrect datetime value: '2019-05-17T11:34:30-04:00'
CREATE TABLE `test_date` (
`t` datetime DEFAULT NULL
)
datetime does not allow a timezone or timezone offset. Some reasonable choices:
convert the timestamp to UTC and store that (my recommendation)
convert the timestamp to the timezone of your database and store that
I had a similar issue, which was caused by the date time field being stored as varchar.
The issue was resolved by doing this:
1) Taking first 10 characters of the timestamp
2) Converting it to date.
where str_to_date(left(my_future_date,10), '%Y-%m-%d') > now()
P.S. this is a hacky way, but i did not want to go through all the code related to this field, and doing all the changes. This solved my issue in 30 seconds. But solution took half an hour to find.
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'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'));
I have some data in a flat file that includes a timestamp field that looks like 1900-01-01 00:00:00. I've been trying to load it into a MySQL table but MySQL is taking it and setting it as 0000-00-00 00:00:00 which is causing issues for me. I've tried running some update commands on the field, but it stays as 0000-00-00 00:00:00.
I know of the from_unixtime function, but how would I use that here? This is the command I'm running to load the data:
load data local infile 'file.txt' into table tgt_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\'' (tbl_name, db_name, timestamp_field, other_col);
The field in question here is timestamp_field.
I've managed to use from_unixtime like update tgt_table set timestamp_field=FROM_UNIXTIME('1900-01-01 00:00:00') where tbl_name = 'test'; but timestamp_field ended up looking like: 1969-12-31 18:31:40
Would appreciate any help on how to format either the column or the SQL to get this inserted correctly
Looks like it might be a column type issue.
From the MySQL 5.6 documentation on time/date fields:
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.
You used the 1900-01-01 in your example which is outside the allowable date range for timestamp. Instead, try using a DATETIME field which has a larger allowable range.
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'.