NOW() returns the current date and time.
CREATE TABLE Orders
(
OrderId int NOT NULL,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (OrderId)
)
Therefor:
INSERT INTO Orders (ProductName) VALUES ('Jarlsberg Cheese')
resulst in :
SELECT NOW(),CURDATE(),CURTIME()
will result in something like this:
| NOW() | CURDATE() | CURTIME() |
|---|---|---|
| 2014-11-22 12:45:34 | 2014-11-22 | 12:45:34 |
CREATE TABLE Orders
(
OrderId int NOT NULL,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (OrderId)
)
Therefor:
INSERT INTO Orders (ProductName) VALUES ('Jarlsberg Cheese')
resulst in :
| OrderId | ProductName | OrderDate |
|---|---|---|
| 1 | Jarlsberg Cheese | 2014-11-22 13:23:44.657 |
_____________________________________________________________________________
MySQL Date Functions
The following table lists the most important built-in date functions in MySQL:
| Function | Description |
|---|---|
| NOW() | Returns the current date and time |
| CURDATE() | Returns the current date |
| CURTIME() | Returns the current time |
| DATE() | Extracts the date part of a date or date/time expression |
| EXTRACT() | Returns a single part of a date/time |
| DATE_ADD() | Adds a specified time interval to a date |
| DATE_SUB() | Subtracts a specified time interval from a date |
| DATEDIFF() | Returns the number of days between two dates |
| DATE_FORMAT() | Displays date/time data in different formats |
_____________________________________________________________________________
SELECT EXTRACT(YEAR FROM OrderDate) AS OrderYear,
EXTRACT(MONTH FROM OrderDate) AS OrderMonth,
EXTRACT(DAY FROM OrderDate) AS OrderDay
EXTRACT(MONTH FROM OrderDate) AS OrderMonth,
EXTRACT(DAY FROM OrderDate) AS OrderDay
FROM Orders
WHERE OrderId=1
WHERE OrderId=1
example
The following SELECT statement:
SELECT DATEDIFF('2014-11-29','2014-11-30') AS DiffDate
will result in this:
| DiffDate |
|---|
| -1 |
_____________________________________________________________________________
No comments:
Post a Comment