DATA no SQL
Autor: Rodrigo Steinhost | Publicado: 2009-11-27T10:17:00-08:00


<!--
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{mso-style-parent:"";
	margin:0cm;
	margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:12.0pt;
	font-family:"Times New Roman";
	mso-fareast-font-family:"Times New Roman";}
h2
	{mso-margin-top-alt:auto;
	margin-right:0cm;
	mso-margin-bottom-alt:auto;
	margin-left:0cm;
	mso-pagination:widow-orphan;
	mso-outline-level:2;
	font-size:18.0pt;
	font-family:"Times New Roman";
	font-weight:bold;}
p
	{mso-margin-top-alt:auto;
	margin-right:0cm;
	mso-margin-bottom-alt:auto;
	margin-left:0cm;
	mso-pagination:widow-orphan;
	font-size:12.0pt;
	font-family:"Times New Roman";
	mso-fareast-font-family:"Times New Roman";}
code
	{font-family:"Courier New";
	mso-ascii-font-family:"Courier New";
	mso-fareast-font-family:"Times New Roman";
	mso-hansi-font-family:"Courier New";
	mso-bidi-font-family:"Courier New";}
pre
	{margin:0cm;
	margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;
	font-size:10.0pt;
	font-family:"Courier New";
	mso-fareast-font-family:"Times New Roman";}
span.database
	{mso-style-name:database;}
span.formalpara-titleitalic
	{mso-style-name:"formalpara-title italic";}
@page Section1
	{size:612.0pt 792.0pt;
	margin:70.85pt 3.0cm 70.85pt 3.0cm;
	mso-header-margin:35.4pt;
	mso-footer-margin:35.4pt;
	mso-paper-source:0;}
div.Section1
	{page:Section1;}
 /* List Definitions */
 @list l0
	{mso-list-id:927739067;
	mso-list-template-ids:1671999840;}
@list l0:level1
	{mso-level-number-format:bullet;
	mso-level-text:;
	mso-level-tab-stop:36.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;
	mso-ansi-font-size:10.0pt;
	font-family:Symbol;}
@list l1
	{mso-list-id:1929338784;
	mso-list-template-ids:-1501560056;}
@list l1:level1
	{mso-level-number-format:bullet;
	mso-level-text:;
	mso-level-tab-stop:36.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;
	mso-ansi-font-size:10.0pt;
	font-family:Symbol;}
@list l2
	{mso-list-id:2075353862;
	mso-list-template-ids:805891334;}
@list l2:level1
	{mso-level-number-format:bullet;
	mso-level-text:;
	mso-level-tab-stop:36.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;
	mso-ansi-font-size:10.0pt;
	font-family:Symbol;}
@list l2:level2
	{mso-level-number-format:bullet;
	mso-level-text:o;
	mso-level-tab-stop:72.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;
	mso-ansi-font-size:10.0pt;
	font-family:"Courier New";
	mso-bidi-font-family:"Times New Roman";}
ol
	{margin-bottom:0cm;}
ul
	{margin-bottom:0cm;}
-->
  
Muitos sistemas fazem várias pesquisas e cálculos utilizando datas e horas. Existem várias formas de fazer isso em delphi, Java e outras linguagens, mas a melhor forma ainda é, no caso de uma adição fixa, fazer isso no SQL. Vou colocar aqui alguns exemplos de funções específicas para DATA e HORA que o firebird possui.


DATEADD()Available in:&nbsp;DSQL, PSQL 
Added in:&nbsp;2.1 
Description:&nbsp;Adds the specified number of years, months, days, hours, minutes, seconds or milliseconds to a date/time value. 
Result type:&nbsp;DATE, TIME or TIMESTAMP
Syntax:&nbsp;
DATEADD ()&nbsp;args&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ::=&nbsp;   TO &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | , , &nbsp;amount&nbsp;&nbsp;&nbsp; ::=&nbsp; an integer expression (negative to subtract)unit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ::=&nbsp; YEAR | MONTH | DAY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| HOUR | MINUTE | SECOND | MILLISECONDdatetime&nbsp; ::=&nbsp; a DATE, TIME or TIMESTAMP expression·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The result type is determined by the third argument.
·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; With DATE arguments, only YEAR, MONTH and DAY can be used. 
·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; With TIME arguments, only HOUR, MINUTE, SECOND and MILLISECOND can be used. 
Examples:&nbsp;
dateadd (28 day to current_date)dateadd (-6 hour to current_time)dateadd (month, 9, DateOfConception)dateadd (minute, 90, time 'now')dateadd (? year to date '11-Sep-1973')




DATEDIFF()Available in:&nbsp;DSQL, PSQL 
Added in:&nbsp;2.1 
Description:&nbsp;Returns the number of years, months, days, hours, minutes, seconds or milliseconds elapsed between two date/time values. 
Result type:&nbsp;BIGINT
Syntax:&nbsp;
DATEDIFF ()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ::=&nbsp;  FROM  TO &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | , , &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ::=&nbsp; YEAR | MONTH | DAY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | HOUR | MINUTE | SECOND | MILLISECOND&nbsp; ::=&nbsp; a DATE, TIME or TIMESTAMP expression·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DATE and TIMESTAMP arguments can be combined. No other mixes are allowed. 
·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; With DATE arguments, only YEAR, MONTH and DAY can be used. 
·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; With TIME arguments, only HOUR, MINUTE, SECOND and MILLISECOND can be used. 
Computation:&nbsp;
·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DATEDIFF doesn't look at any smaller units than the one specified in the first argument. As a result, 
o&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; “datediff (year, date '1-Jan-2009', date '31-Dec-2009')” returns 0, but 
o&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; “datediff (year, date '31-Dec-2009', date '1-Jan-2010')” returns 1 
·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; It does look, however, at all the bigger units. So: 
o&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; “datediff (day, date '26-Jun-1908', date '11-Sep-1973')” returns 23818 
·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A negative return value indicates that moment2 lies before moment1. 
Examples:&nbsp;
datediff (hour from current_timestamp to timestamp '12-Jun-2059 06:00')datediff (minute from time '0:00' to current_time)datediff (month, current_date, date '1-1-1900')datediff (day from current_date to cast(? as date))





http://www.firebirdsql.org/