174

Is there a value or command like DATETIME that I can use in a manual query to insert the current date and time?

INSERT INTO servers (
  server_name, online_status, exchange, disk_space, network_shares
) VALUES(
  'm1', 'ONLINE', 'ONLINE', '100GB', 'ONLINE' 'DATETIME' 
)

The quoted DATETIME value at the end is where I want to add the current date and time.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Samuel Nicholson
  • 3,587
  • 3
  • 20
  • 37
  • http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html – Colandus Oct 08 '13 at 11:19
  • 8
    NOW(), see https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_now ... And please, next time put at least some research effort into your question, this can be easily googled (therefore I just downvoted this question) – dirkk Oct 08 '13 at 11:19
  • My question was more aimed at how I should use it in the syntax I posted above. – Samuel Nicholson Oct 08 '13 at 11:21
  • 1
    See franciscos answer for that. But this is very, very basic, it is just a function call. Therefore, again, you should put more research effort into the issue before posting a question and also you might want to read some instructions on SQL. – dirkk Oct 08 '13 at 11:23
  • 9
    ironic. when putting effort into finding the answer you get to ... this answer! – Zach Smith Jul 12 '14 at 22:18
  • forgot with this, long time not playing with database – Steven Sugiarto Wijaya Sep 23 '16 at 07:46
  • @dirkk I agree with you about the research effort. But I think it's a valid question because if you would type `INSERT INTO sometable (\`datetime\`) VALUES ( NOW() ) ` in phpmyadmin's sql tab then phpmyadmin's hint would show an error icon with the message: `A comma or a closing bracket was expected near (`. This is confusing, because the sql query is valid but phpmyadmin says it's not. – Julian Feb 21 '19 at 10:13

10 Answers10

305

You can use NOW():

INSERT INTO servers (server_name, online_status, exchange, disk_space, network_shares, c_time)
VALUES('m1', 'ONLINE', 'exchange', 'disk_space', 'network_shares', NOW())
Yuri
  • 3,082
  • 3
  • 28
  • 47
francisco.preller
  • 6,559
  • 4
  • 28
  • 39
34

Use CURRENT_TIMESTAMP() or now()

Like

INSERT INTO servers (server_name, online_status, exchange, disk_space,
network_shares,date_time) VALUES('m1','ONLINE','ONLINE','100GB','ONLINE',now() )

or

INSERT INTO servers (server_name, online_status, exchange, disk_space,
network_shares,date_time) VALUES('m1', 'ONLINE', 'ONLINE', '100GB', 'ONLINE'
,CURRENT_TIMESTAMP() )

Replace date_time with the column name you want to use to insert the time.

Ankit Sharma
  • 3,923
  • 2
  • 29
  • 49
30

Even though there are many accepted answers, I think this way is also possible:

Create your 'servers' table as following :

CREATE TABLE `servers`
(
      id int(11) NOT NULL PRIMARY KEY auto_increment,
      server_name varchar(45) NOT NULL,
      online_status varchar(45) NOT NULL,
      _exchange varchar(45) NOT NULL, 
      disk_space varchar(45) NOT NULL,
      network_shares varchar(45) NOT NULL,
      date_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
);

And your INSERT statement should be :

INSERT INTO servers (server_name, online_status, _exchange, disk_space, network_shares)
VALUES('m1', 'ONLINE', 'ONLINE', '100GB', 'ONLINE');

My Environment:

Core i3 Windows Laptop with 4GB RAM, and I did the above example on MySQL Workbench 6.2 (Version 6.2.5.0 Build 397 64 Bits)

simhumileco
  • 31,877
  • 16
  • 137
  • 115
Erandi
  • 301
  • 3
  • 3
7

Yes, you can use the CURRENT_TIMESTAMP() command.

See here: Date and Time Functions

simhumileco
  • 31,877
  • 16
  • 137
  • 115
Alec.
  • 5,371
  • 5
  • 34
  • 69
5

The correct answer is SYSDATE().

INSERT INTO servers (
    server_name, online_status, exchange, disk_space,
    network_shares, date_time
)
VALUES (
    'm1', 'ONLINE', 'ONLINE', '100GB', 'ONLINE', SYSDATE()
);

We can change this behavior and make NOW() behave in the same way as SYSDATE() by setting sysdate_is_now command line argument to True.

Note that NOW() (which has CURRENT_TIMESTAMP() as an alias), differs from SYSDATE() in a subtle way:

SYSDATE() returns the time at which it executes. This differs from the behavior for NOW(), which returns a constant time that indicates the time at which the statement began to execute. (Within a stored function or trigger, NOW() returns the time at which the function or triggering statement began to execute.)

As indicated by Erandi, it is best to create your table with the DEFAULT clause so that the column gets populated automatically with the timestamp when you insert a new row:

date_time datetime NOT NULL DEFAULT SYSDATE()

If you want the current date in epoch format, then you can use UNIX_TIMESTAMP(). For example:

select now(3), sysdate(3), unix_timestamp();

would yield

+-------------------------+-------------------------+------------------+
| now(3)                  | sysdate(3)              | unix_timestamp() |
+-------------------------+-------------------------+------------------+
| 2018-11-27 01:40:08.160 | 2018-11-27 01:40:08.160 |       1543282808 |
+-------------------------+-------------------------+------------------+

Related:

codeforester
  • 39,467
  • 16
  • 112
  • 140
2
INSERT INTO servers (server_name, online_status, exchange, disk_space, network_shares)
VALUES('m1','ONLINE','exchange','disk_space','network_shares', NOW())
AT82
  • 71,416
  • 24
  • 140
  • 167
1

In database design, iIhighly recommend using Unixtime for consistency and indexing / search / comparison performance.

UNIX_TIMESTAMP() 

One can always convert to human readable formats afterwards, internationalizing as is individually most convenient.

FROM_ UNIXTIME (unix_timestamp, [format ])
tony gil
  • 9,424
  • 6
  • 76
  • 100
1

İf you make change default value to CURRENT_TIMESTAMP it is more effiency,

ALTER TABLE servers MODIFY COLUMN network_shares datetime NOT NULL DEFAULT CURRENT_TIMESTAMP;
Ferhat KOÇER
  • 3,890
  • 1
  • 26
  • 26
0

You can use not only now(), also current_timestamp() and localtimestamp(). The main reason of incorrect display timestamp is inserting NOW() with single quotes! It didn't work for me in MySQL Workbench because of this IDE add single quotes for mysql functions and i didn't recognize it at once )

Don't use functions with single quotes like in MySQL Workbench. It doesn't work.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
obohovyk
  • 482
  • 2
  • 5
  • 12
-9
$rs = $db->Insert('register',"'$fn','$ln','$email','$pass','$city','$mo','$fil'","'f_name','l_name=','email','password','city','contact','image'");
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91