SQL YEAR,MONTH,DAY,NOW
Example Data Table : Orders (All of the below examples use this table as datasource)
Keyid | OrderNo | CustomerName | SalePrice | Quantity | OrderDate |
---|---|---|---|---|---|
1 | 20101213095 | John | 3500 | 10 | 2010-12-13 |
2 | 20100620135 | Jimmy | 2509 | 22 | 2010-06-20 |
3 | 20100620392 | Vincent | 7200 | 5 | 2010-06-20 |
4 | 20100530006 | John | 9200 | 50 | 2010-05-30 |
5 | 20100515129 | Vincent | 100 | 50 | 2010-05-29 |
Example
To get year, month, day from a date column
Sytax : SELECT YEAR(column_name), MONTH(column_name), DAY(column_name) FROM table_name
SELECT YEAR(OrderDate), MONTH(OrderDate), DAY(OrderDate), OrderDate FROM Orders
will output
Year | Month | Day | OrderDate |
---|---|---|---|
2010 | 12 | 13 | 2010-12-13 |
2010 | 6 | 20 | 2010-06-20 |
2010 | 6 | 20 | 2010-06-20 |
2010 | 5 | 30 | 2010-05-30 |
2010 | 5 | 29 | 2010-05-29 |
Example
To get current time
Sytax : SELECT NOW()
SELECT NOW()
will output
Now() |
---|
2012-01-07 22:27:40 |