Last active
February 9, 2017 22:02
-
-
Save claudiohilario/2aac7ba8f57cd2fc43363401deceeeeb to your computer and use it in GitHub Desktop.
Trabalhar com datas em SQL (Resumo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT CURDATE(); | |
--Resultado: 2016-10-24 | |
SELECT CURDATE()+0; | |
--Resultado: 20161024 | |
SELECT DATE_ADD('2016-10-23 23:59:59', INTERVAL 1 DAY); | |
--Resultado: 2016-10-24 23:59 | |
SELECT DATEDIFF('2016-11-23 23:59:59','2016-10-31 23:59:59'); | |
--Resultado: 23 | |
SELECT SUBDATE('2016-10-31 23:59:59', INTERVAL 10 DAY); | |
--Resultado: 2016-10-21 23:59:59 | |
SELECT DAYOFWEEK('1992-05-29'); | |
--Resultado: 6 | |
SELECT DAYOFYEAR(CURDATE()); | |
--Resultado: 298 | |
SELECT NOW(); | |
--Resultado: 2016-10-24 03:43:23 | |
SELECT SYSDATE(); | |
--Resultado: 2016-10-24 03:44:07 | |
--Formatar data com DATE_FORMAT | |
--http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format | |
SELECT id, DATE_FORMAT( 'date' , '%d/%c/%Y %H:%i:%s' ) AS 'date' FROM 'dates' | |
/* | |
* Querys Interessantes com datas | |
*/ | |
SELECT * FROM dados WHERE DATEDIFF(now(), data_entrada) >= 90 | |
SELECT * FROM dados WHERE DATEDIFF(data_entrada, now()) >= 90 | |
SELECT titulo | |
FROM eventos | |
WHERE '2014-02-01' BETWEEN date(inicio) AND date(fim) | |
SELECT titulo | |
FROM eventos | |
WHERE '2014-02-01' >= date(inicio) | |
AND '2014-02-01' <= date(fim) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment