tags:

views:

206

answers:

6

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?

thanks,

A: 

For SQL Server use GetDate() or current_timestamp. You can format the result with the Convert(dataType,value,format). Tag your question with the correct Database Server.

Saif Khan
+8  A: 

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

Nathan Loding
+1  A: 

I want my datetime, and I want it now()!

For MySQL, anyway.

Matthew Jones
That's cute... Doesn't work with Microsoft though: "I want my date, and I want it GetDate()!"
Wadih M.
Sure it does. "Well, mister, you can just GetDate() yourself from now on!"
Matthew Jones
+4  A: 

NOW() returns 2009-08-05 15:13:00

CURDATE() returns 2009-08-05

CURTIME() returns 15:13:00

sixfoottallrabbit
A: 
  1. GETDATE() or GETUTCDATE() are now superseded by the richer SYSDATETIME, SYSUTCDATETIME, and SYSDATETIMEOFFSET (in SQL 2008)
  2. Yes, I don't think ANSI has ever declared anything, and so each manufacturer has their own.
  3. That would be NOW()

Hope this helps...

Rob

Rob Farley
+5  A: 

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

(As SixFootTallRabbit says)

Eduardo Molteni
+1 -- better answer than mine. I kinda shorted myself.
Nathan Loding