how to insert date in sql?

Today, We want to share with you how to insert date in sql.In this post we will show you create date column in sql, hear for how to insert date in sql in dd/mm/yyyy format we will give you demo and example for implement.In this post, we will learn about how to insert date in mysql using php? with an example.

How to insert date values into table?

Date and Time Manipulation

Type Default format Allowable values
DATE YYYY-MM-DD 1000-01-01 to 9999-12-31
TIME HH:MM:SS or HHH:MM:SS -838:59:59 to 838:59:59
DATETIME YYYY-MM-DD HH:MM:SS 1000-01-01 00:00:00 to 9999-12-31 23:59:59
TIMESTAMP YYYY-MM-DD HH:MM:SS 1970-01-01 00:00:00 to 2037-12-31 23:59:59
YEAR YYYY 1901 to 2155

SQL Working With Dates

Example 1:

SELECT * FROM products WHERE productDate='2021-11-11'

Tracking Row Creating or Modification Times
Syntax for MySQL Database

INSERT INTO members (name, birth_date, created_at)
VALUES ('Gondaliya shital', '1992-04-16', NOW());
CREATE TABLE members (
    id INT(4) NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL UNIQUE,
    birth_date DATE NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

Formatting Dates or Times

mysql> SELECT name, DATE_FORMAT(birth_date, '%M %e, %Y') FROM members;

Extracting Parts of Dates or Times

mysql> SELECT name, YEAR(birth_date) FROM members;

mysql> SELECT name, DAYOFMONTH(birth_date) FROM members;

Full Code

Example 3:

SQL>
SQL>
SQL> CREATE TABLE products (
  2       name  VARCHAR2(25),
  3       price NUMBER(4,2),
  4       start_date DATE);

Table created.

SQL>
SQL>
SQL> INSERT INTO products VALUES (NULL, 2.5, '29-JUN-21');

1 row created.

SQL>
SQL> INSERT INTO products VALUES ('Product Name 3', null, '10-DEC-22');

1 row created.

SQL>
SQL> INSERT INTO products VALUES (NULL, NULL, '31-AUG-23');

1 row created.

SQL>
SQL> SELECT * FROM products;

NAME                           PRICE START_DAT
------------------------- ---------- ---------
                                 2.5 29-JUN-21
Product Name 3                       10-DEC-22
                                     31-AUG-23

3 rows selected.

SQL>
SQL>
SQL> drop table products;

Table dropped.

SQL>
SQL>

I hope you get an idea about date datatype in sql.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment