MySQL - Create table with a specific TIMESTAMP format - mysql

Is there any way to create a table with a specific TIMESTAMP format (HH:mm:00) where I need to fix sec to 00.
CREATE TABLE `published` (
`time_pub` timestamp() NULL DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

You can't change the way the timestamp is stored. Only the way it is presented in your select statements.
Another solution would be to store the data in a hour and minute column having an integer data type.

Although you can't specify the time format when create table, you can use date_format function in MySQL to fix the second part to 00 when insert data to database.
insert into TABLE_NAME values (date_format(now(), '%H:%m:00'));

Related

How to store unix timestamp as int with default and on update?

I have been updating my MySQL tables with the following:
ALTER TABLE logs ADD COLUMN updateTimeStamp timestamp DEFAULT current_timestamp() ON UPDATE current_timestamp;
This stores the timestamp in the format of
2021-12-29 15:21:34
I tried originally to do the alter like so:
ALTER TABLE logs ADD COLUMN updateTimeStamp timestamp DEFAULT unix_timestamp() ON UPDATE unix_timestamp();
so that could store like 12121232, however that results in an error.
Is there anyway I can achieve the default and on update and store the timestamp in the format of 1212112, instead of the human readable datetime?
I know I can do SELECT unix_timestamt(columnname), but ideally I don't want to do that.
If you want to automatically get an integer when you select the column, you need to make it an int (or int unsigned) type. You can set its default with default (unix_timestamp()) (the extra parentheses are needed when not using one of the historically allowed default values). And you will need to add a trigger to set it on update.
But I suggest you not do that; just use a timestamp type. You just make future trouble for yourself by not using the type designed to store timestamps.

MySQL - How to insert the current date as Unix time stamp?

I have a table called 'products' with some columns. One of them is called 'date' and should contain the date on which the product was added as Unix time stamp. Normally I would use the NOW() function but since the value needs to be an integer and I can't cast the NOW() function as integer, I need to find another way. Any ideas ?
Modify the column to be of type TIMESTAMP and set it's default value (if you want to set the column to the date it was added on) to CURRENT_TIMESTAMP.
ALTER TABLE `products` MODIFY `date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;
TIMESTAMP is not a string but an integer timestamp, it is just displayed as a string and you can use operators on it.
With DEFAULT CURRENT_TIMESTAMP you don't need to insert the date, mysql will do it for you.
MySQL has a function with the surprising name unix_timestamp:
INSERT INTO `products`
(`name`, `date`)
VALUES ('some name', UNIX_TIMESTAMP())

How to insert the current system date into a db column using mysql

I have three *date fields in a table defined as VARCHAR(45) with a default value of '00-00-0000'. Each of the fields needs the default value of the current system time when a record is created.
I have changed one of the fields from VARCHAR(45) to TIMESTAMP with default of CURRENT_TIMESTAMP, which works great. I think I can only have a single field with Datatype TIMESTAMP per table.
How do I handle the other 2 fields in the tables? I would like to *CURRENT_TIMESTAMP them too.
Thanks
Use the NOW() function:
INSERT INTO yourtable (field1, field2) VALUES ('blahblah', now());
You can use "NOW()" in a MySQL query to fill in the current time for a timestamp column.
You can write Now() to the fields
INSERT INTO table (right_now)
VALUES (Now()):

Choosing correct MySQL query

In my project I am using one MySQL table to store actual information for every id (unnecessary fields are omitted):
CREATE TABLE mytable (
`id` varchar(8) NOT NULL,
`DateTime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=MyISAM
I need to update rows in this table and insert new DateTime only if this incoming DateTime is newer. But at the same time I do not know if the row with such id already exists in the table. Here is the pseudo-code of what I would like to achieve:
if id does not exist
insert id and DateTime
else if newDateTime > DateTime
update DateTime
I tried to use replace, but as far as I know it is not possible to compare fields from within replace body.
I tried to use update, but if row does not already exist - new information is not being inserted.
Is it possible to do something like this in one query? And if not - what could be the workaround?
PS: I do not think that I have permission to add stored procedures, so this should be a last resort.
I've tested this and it works. And I'm pretty proud of it.
INSERT INTO mytable (`id`,`DateTime`)
VALUES ('your_new_id','your_new_DateTime')
ON DUPLICATE KEY UPDATE `DateTime` = IF(`DateTime` < 'your_new_DateTime', 'your_new_DateTime', `DateTime`)

MYSQL: How to convert VARCHAR column containing dates to a DATE column?

I have a large table of birthdays that I want to convert from a varchar column to a date column.
The table SQL is:
CREATE TABLE `birthdays` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`uid` bigint(20) DEFAULT NULL,
`birthday_date` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM
The birthday date is stored in one of two ways: strings like "05/26/1994" or sometimes "03/14" (for people who don't want to reveal their birth year).
What is the best way to create another table and store the birthdays in a date column? Is it possible to do this just using MySQL (and avoid using PHP or some other intermediary)?
I have found a STR_TO_DATE function in MySQL. Should I use this?
Thanks!
SELECT IF(birthday_date RLIKE '[0-9]{2}/[0-9]{2}/[0-9]{4}',STR_TO_DATE(birthday_date,'%m/%d/%Y'),STR_TO_DATE(birthday_date,'%m/%d'));
This will result in dates like 0000-03-14 for rows that have no year entered. Your server needs to be configured to allow invalid dates though (see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html )
If you convert your column to DateTime then you will not be able to store dates like "03/14" in which year is missing. So instead I suggest to keep this as it is and probably have another column for storing the dateTime if you really need that.
Also have internal trigger to convert the datestrings from varchar column to dateTime column.
yes, you have to use the method STR_TO_DATE for your purpose.