#14 - DATAS
Autor: Bruno Martins | Publicado: 2011-05-27T11:48:00-03:00

Algumas funções bem interessante sobre datas do SQL Server, alguma delas podem ser úteis para sua vida.


select DATEPART(dw, GETDATE()) as 'DIA'DIA-----------6


SELECT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GETDATE()&nbsp;AS&nbsp;'Today',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GETDATE()&nbsp;+&nbsp;10&nbsp;AS&nbsp;'10 dias a mais',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GETDATE()&nbsp;-&nbsp;10&nbsp;AS&nbsp;'10 dias atrás'

Today &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10 dias a mais &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10 dias atrás
----------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ----------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-----------------------
2011-05-27 11:19:11.570 &nbsp;2011-06-06 11:19:11.570 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2011-05-17 11:19:11.570

SELECT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DAY&nbsp; ( GETDATE() ) AS 'DIA',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MONTH( GETDATE() ) AS 'Mês',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; YEAR ( GETDATE() ) AS 'Ano'

DIA &nbsp; &nbsp; &nbsp; &nbsp; Mês &nbsp; &nbsp; &nbsp; &nbsp; Ano
----------- ----------- -----------
27 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2011






SELECT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DATEPART(DAY,&nbsp;&nbsp;&nbsp;GETDATE())&nbsp;AS&nbsp;'Dia',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DATEPART(MONTH,&nbsp;GETDATE())&nbsp;AS&nbsp;'Mês',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DATEPART(YEAR,&nbsp;&nbsp;GETDATE())&nbsp;AS&nbsp;'Ano',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DATEPART(HOUR,&nbsp;&nbsp;&nbsp;GETDATE())&nbsp;AS&nbsp;'Hora',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DATEPART(MINUTE,&nbsp;GETDATE())&nbsp;AS&nbsp;'Minuto',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DATEPART(SECOND,&nbsp;GETDATE())&nbsp;AS&nbsp;'Segundo'



Dia &nbsp; &nbsp; Mês &nbsp; &nbsp; &nbsp; Ano &nbsp; &nbsp; &nbsp; Hora &nbsp; &nbsp; &nbsp; &nbsp;Minuto &nbsp; &nbsp; &nbsp;Segundo------ --------- --------- ---------- ----------- -----------27 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp;2011 &nbsp; &nbsp; &nbsp; &nbsp;11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;35 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10








SELECT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GETDATE() AS 'Today',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DATEADD(DAY,&nbsp;&nbsp; 10, GETDATE()) AS '10 Dias a mais',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DATEADD(DAY,&nbsp; -10, GETDATE()) AS '10 Dias atrás',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DATEADD(MONTH,&nbsp; 1, GETDATE()) AS 'Próximo Mês',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DATEADD(MONTH, -1, GETDATE()) AS 'Mês Anterior',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DATEADD(YEAR,&nbsp;&nbsp; 1, GETDATE()) AS 'Próximo Ano',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DATEADD(YEAR,&nbsp; -1, GETDATE()) AS 'Ano Passado'






DECLARE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Today&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DATETIME&nbsp;=&nbsp;GETDATE()DECLARE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@IDay&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DATETIME&nbsp;=&nbsp;'2011-05-27 08:30:00'
SELECT&nbsp;DATEDIFF(DAY,&nbsp;@Today,&nbsp;@IDay)&nbsp;AS&nbsp;'Days Left',&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;DATEDIFF(MONTH,&nbsp;@Today,&nbsp;@IDay)&nbsp;AS&nbsp;'Months Left',&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;DATEDIFF(YEAR,&nbsp;@Today,&nbsp;@IDay)&nbsp;AS&nbsp;&nbsp;'Years Left',&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;DATEDIFF(QUARTER,&nbsp;@Today,&nbsp;@IDay)&nbsp;AS&nbsp;&nbsp;&nbsp;'Quarters Left',&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;DATEDIFF(HOUR,&nbsp;@Today,&nbsp;@IDay)&nbsp;AS&nbsp;&nbsp;&nbsp; &nbsp;'Hours Left',&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;DATEDIFF(MINUTE,&nbsp;@Today,&nbsp;@IDay)&nbsp;AS&nbsp;&nbsp;&nbsp;&nbsp;'Minutes Left',&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;DATEDIFF(SECOND,&nbsp;@Today,&nbsp;@IDay)&nbsp;AS&nbsp;&nbsp;&nbsp;&nbsp;'Seconds Left'

Days Left &nbsp; Months Left Years Left &nbsp;Quarters Left Hours Left &nbsp;Minutes Left Seconds Left----------- ----------- ----------- ------------- ----------- ------------ -------------385 &nbsp; &nbsp; &nbsp; &nbsp;-12 &nbsp; &nbsp; &nbsp; &nbsp; -1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-9243 &nbsp; &nbsp; &nbsp; -554590 &nbsp; &nbsp; &nbsp;-33275437