Datetime value is not accepted by mysql database [duplicate] - mysql

This question already has answers here:
Insert date and time into Mysql
(6 answers)
Closed 7 years ago.
I have a mysql table that I created like this:
CREATE TABLE `Table` (
 `Table_id` int(11) NOT NULL AUTO_INCREMENT,
 `Table_date` datetime NOT NULL,
`Table_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP,
 PRIMARY KEY (`Played_id`))
ENGINE=MyISAM AUTO_INCREMENT=18 DEFAULT CHARSET=utf8
Now I try to call the INSERT query:
INSERT INTO Table (`Table_date`) VALUES ('04/12/2015 16:50:35')
and it doesn't work. I get the confirmation that one row was inserted, but all I see is value 0000-00-00 00:00:00 as a Table_date. The timestamp in Table_modified is fine, it's current value, but only zeros in Table_date really bugs me. I know it's impossible to store two timestamps in database, that's why I've decided to switch and use the type Datetime as for Table_date, so far with no success. How can I fix it and fill that column with the time provided?

You have the format for the date part wrong. Try this:
INSERT INTO Table (`Table_date`) VALUES ('2015-04-12 16:50:35')

Related

How do I fix this MYSQL//Phpmyadmin error? [duplicate]

This question already has answers here:
#1292 - Incorrect date value: '0000-00-00' [duplicate]
(3 answers)
Closed 1 year ago.
So I tried to do a ALTER TABLE command to my table to add a date column that stores what time a post was made. Whenever I enter the SQL code, it pops up this error in PhpMyAdmin. I'm a beginner and I would really like if someone could help me.
Original code:
ALTER TABLE posts
ADD date datetime not null;
Error that pops up:
#1292 - Incorrect date value: '0000-00-00' for column 'website' . 'posts' . 'date' at row 1
Give a default value
ALTER TABLE posts ADD `date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
Or if default value is not desired, add the column allowing NULL, update with appropriate values, and change the column to NOT NULL
ALTER TABLE posts ADD `date` datetime
;
UPDATE posts
SET `date` = NOW() -- or any suitable values
;
ALTER TABLE posts CHANGE `date` `date` datetime NOT NULL
;
You are adding a column, that can't be null. So what value do the existing rows get?
You need to either specify a default value, or allow null until its populated somehow.

How to update current time stamp in MS SQL Server? [duplicate]

This question already has answers here:
Need a datetime column in SQL Server that automatically updates when the record is modified
(5 answers)
Closed 1 year ago.
What is the equivalent of the following in MS SQL server syntax?
CREATE TABLE `new_table` (
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
I have tried:
update myTable set updated = CURRENT_TIMESTAMP;
Any update on table with timestamp data type will automatically update it's row timestamp value. If anything is missing in your question, please add more data.

How do I set the default value of a column to today's date? [duplicate]

This question already has answers here:
CURRENT_DATE/CURDATE() not working as default DATE value
(10 answers)
Closed 5 years ago.
I'm currently working on a small project in which I need to insert some data into database tables. What I want to do is to set the default value of a certain column to today's date, but only to today's date.
Is this even possible? I already tried the datatypes DATETIME and TIME and its functions CURRENT_TIMESTAMP() and TIMESTAMP() to set their value. Problem is that I only want to have the today's date in my column, not the date and the time. Does anyone know if that's possible?
Here an example:
CREATE TABLE IF NOT EXISTS Bestellung(
BestellNr MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
Bestelldatum DATE DEFAULT ??? NOT NULL,
FahrerID TINYINT UNSIGNED NOT NULL,
KundenNr SMALLINT UNSIGNED NOT NULL,
FOREIGN KEY(FahrerID) REFERENCES
Fahrer(FahrerID) ON UPDATE CASCADE,
FOREIGN KEY(KundenNr) REFERENCES
Kunde(KundenNr) ON UPDATE CASCADE,
PRIMARY KEY(BestellNr),
INDEX(Bestelldatum)
)
In MySL you could use the function CURDATE() to retun only the date as YYYY-MM-DD.
Reference: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html

Insert timestamp into a database + 7 days

I am trying to create a table in MySQL that has a timestamp field that is 7 days ahead of now() is there a way to do this?
This is my current code
CREATE TABLE tbl_reg
(
reg_id int(7) NOT NULL auto_increment KEY,
user_id int(7) NOT NULL,
registration_key char(50) NOT NULL,
registration_expires timestamp default now()+7days,
stamp_created timestamp default now()
);
Can anyone help as i cant find anything like this on the mysql site and wondered if there was a way to do it?
There are a number of date/time functions in MySQL that will do the trick here.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
registration_expires=DATE_ADD(NOW(), INTERVAL 7 DAY)
You can't set an expression as a default, though - you'll need to do it in your INSERT queries. Notice that even if your expr value is > 1 there is no plural used for the unit value.
Or you could create a view from a query where you add the interval, or when you query the db always add the 7 days interval.

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