Tuesday, February 28, 2017

4 - DATE functions

NOW() returns the current date and time.

SELECT NOW(),CURDATE(),CURTIME()
will result in something like this:
NOW()CURDATE()CURTIME()
2014-11-22 12:45:342014-11-2212:45:34

CREATE TABLE Orders
(
OrderId int NOT NULL,
ProductName varchar(50NOT NULL,
OrderDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (OrderId)
)



Therefor:
INSERT INTO Orders (ProductName) VALUES ('Jarlsberg Cheese')

resulst in :

OrderIdProductNameOrderDate
1Jarlsberg Cheese2014-11-22 13:23:44.657

_____________________________________________________________________________

MySQL Date Functions

The following table lists the most important built-in date functions in MySQL:
FunctionDescription
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 OrderDateAS OrderYear,
EXTRACT(MONTH FROM OrderDateAS OrderMonth,
EXTRACT(DAY FROM OrderDateAS OrderDay
FROM Orders
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