tags:

views:

74

answers:

3

Hello,

SQL beginner here !

What is the most convenient way to create datetime objects within an SQL function, especially generating a datetime object for a given day, month, and year?

Thanks !

+5  A: 

Everything you want to know about MySQL datetime functions is right here. Well, probably most everything.

Kaleb Brasee
Thanks for the link.
Amadeus45
+2  A: 

In MySQL

CAST('YYYY-MM-DDThh:mm:ss:uuuuuu' AS datetime)

where:
YYYY: Year
MM : Month
DD : Day
hh : Hour
mm : Minutes
ss : Seconds
uuuuuu : Microseconds

EDIT: I changed from mmm (miliseconds) to uuuuuu (microseconds) since MySQL suports 6 digits

Carlos Muñoz
A: 

Given a datetime column: INSERT INTO Table ('datetime_column') VALUES (CURDATE( ))

Given a timestamp: INSERT INTO Table ('timestamp_column') VALUES (CURTIME())

Using unix time: INSERT INTO Table ('timestamp_column') VALUES (UNIX_TIMESTAMP())

Derek Adair