How to create empty datetime format in mysql - mysql

The mysql default datetime format when null is 1970-01-01 07:00:00
Is it possible to have an empty string (" ") instead of a null when loading it into a data grid? It would behave as an empty column/cell.

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.

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.)

conversion of epoch_time gives '1970-01-01' in mysql

I've column name epoch_time with a varchar(255) which is having approx 40,000 epoch time in it and I've tried to convert all of them into a equivalent date with this query UPDATE table_name SET converted_into_date = FROM_UNIXTIME(epoch_time);
but it is giving 1970-01-01 05:30:00 in all rows and the datatype of converted_into_date column is varchar. I've tried all the datatype but then it is returning null . working in maria_db.
Please try
SELECT FROM_UNIXTIME(1526019841742682/1000000)

how to convert datetime automatically from 00/00/0000 00:00:00 format to 0000-00-00 00:00:00 format on CSV

I have the following csv
January,December,,,Blaize,Wildsights,Blaize,Wildsights,61,1,30,,,,,,,,,,10/09/2013 20:10:52,10/09/2013 20:11:05
January,December,,,Michael,Wildsights,Michael,Wildsights,61,2,31,,,,,,,,,,10/09/2013 20:11:03,10/09/2013 20:11:05
........
....
..
After importing it using phpmyadmin I saw that the timestamps have not been imported.
If it is not possible to keep the timestamps as 00/00/0000 00:00:00 how can I convert datetime automaticaly from 00/00/0000 00:00:00 format to 0000-00-00 00:00:00 format on CSV. I have thousand rows
Add a tempDate column of NVARCHAR type to your mySql database.
import your CSV telling phpmyadmin to import your problematic dates into the tempDate column instead of your actual date column. This will succeed, because tempDate is NVARCHAR, so it will accept anything.
Consult this: how to convert a string to date in mysql? on how to execute an UPDATE statement on your database to populate your actual date column from the tempDate column.
remove the tempDate column.

How do I load a MySQL timestamp field into a specific format from a flat file?

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'.