SQL INSERT DATE NOW

In this short article I show you how to insert the current date and time into MySQL.

Specifically, we will see the 3 ways of get current date and time in mysql, either conjoined with CURDATETIME or separately; the current date in SQL with the CURDATE() function and the current time with CURTIME().

Content

  • SQL INSERT from DATETIME with NOW
    • Example of Insert current DATETIME
  • insert current date in mysql
    • SQL INSERT CURDATE Example
  • How to insert current time in MySQL?
    • Example Insert Current Time SQL

SQL INSERT from DATETIME with NOW

If you use columns of the DATETIME type and you want to enter the current date, you must use a special function for it.

In SQL the function to get current date and time is NOW().

It is a simple function and it is used like any other SQL function.

Let’s look at an example for clarity.

Example of Insert current DATETIME

INSERT INTO tabla_1 ( titulo, fecha_hora_actual )
VALUES ( 'insert datetime sql now', NOW() );

Clarifications on the insertion of the DATETIME:

  1. The spine current_date_time It is DATETIME type.
  2. Although in SQL both dates and times are written in quotes, when using a function, and in particular the NOW function, the quotes are not necessary.
  3. The NOW function returns the server date and time where the database is running. Thus, if the server is in Portugal, the time will be GMT.

insert current date in mysql

Insert into a DBMS SQL table MySQL current date It is really simple, for this we must use the sql CURDATE() function.

The function SAVE MySQL returns the date of the instant in which the call is made.

The format of the date obtained is the same that the database uses to store dates, that is, YYYY-MM-DD.

SQL INSERT CURDATE Example

In the following example of CURDATE SQL you can see how easy it is to use:

INSERT INTO tabla ( titulo, columna_fecha )
VALUES ( 'sql curdate ejemplo', CURDATE() );

Regarding the example, although it is extremely simple, I would like to leave some observations:

  1. Although quotes are used when entering dates in SQL, when using the CURDATE function it is not necessary.
  2. Although you may already know this, since CURDATE is an SQL function you must specify the parentheses: CURDATE().

An alternative to the CURDATE() function is CURRENT_DATE(). Its use and operation are identical with the difference in its name.

How to insert current time in MySQL?

As well as to insert dates in SQL there is a native function in MySQL, for enter current time there is also a function.

The function to insert the current time is CURTIME().

Similar to the previous method, the CURTIME method returns the time in the format used by SQL: HH:II:SS

Example Insert Current Time SQL

The following example is about inserting a title and current time.

INSERT INTO tabla ( titulo, columna_time )
VALUES ( 'sql curtime ejemplo', CURTIME() );

The time column on which the insertion of the current time falls is of the TIME type.

Leave a Reply