Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get current datetime in SQL?

Want to get current datetime to insert into lastModifiedTime column. I am using MySQL database. My questions are:

  1. is there a function available in SQL? or

  2. it is implementation depended so each database has its own function for this?

  3. what is the function available in MySQL?

like image 666
5YrsLaterDBA Avatar asked Aug 05 '09 14:08

5YrsLaterDBA


People also ask

How do I get the current timestamp in SQL query?

MySQL CURRENT_TIMESTAMP() Function The CURRENT_TIMESTAMP() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS. uuuuuu (numeric).

How do I get the current date in SQL without the time?

First Convert the date to float (which displays the numeric), then ROUND the numeric to 0 decimal points, then convert that to datetime.

Is there a today function in SQL?

SQL Server provides several different functions that return the current date time including: GETDATE(), SYSDATETIME(), and CURRENT_TIMESTAMP.


2 Answers

Complete answer:

1. Is there a function available in SQL?
Yes, the SQL 92 spec, Oct 97, pg. 171, section 6.16 specifies this functions:

CURRENT_TIME       Time of day at moment of evaluation CURRENT_DATE       Date at moment of evaluation CURRENT_TIMESTAMP  Date & Time at moment of evaluation 

2. It is implementation depended so each database has its own function for this?
Each database has its own implementations, but they have to implement the three function above if they comply with the SQL 92 specification (but depends on the version of the spec)

3. What is the function available in MySQL?

NOW() returns 2009-08-05 15:13:00   CURDATE() returns 2009-08-05   CURTIME() returns 15:13:00   
like image 154
Eduardo Molteni Avatar answered Sep 21 '22 12:09

Eduardo Molteni


I always just use NOW():

INSERT INTO table (lastModifiedTime) VALUES (NOW()) 

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_now

like image 40
Nathan Loding Avatar answered Sep 22 '22 12:09

Nathan Loding