{"id":361,"date":"2010-07-08T11:56:59","date_gmt":"2010-07-08T10:56:59","guid":{"rendered":"http:\/\/www.aleprex.it\/wordpress\/?p=361"},"modified":"2010-07-08T11:56:59","modified_gmt":"2010-07-08T10:56:59","slug":"sql-funzione-per-calcolare-leta-3","status":"publish","type":"post","link":"https:\/\/www.aleprex.it\/wordpress\/sql-funzione-per-calcolare-leta-3.html","title":{"rendered":"SQL : Funzione per calcolare l&#8217;et\u00e0"},"content":{"rendered":"<p>Calcola l&#8217;et\u00e0 fornendo una data e confrontandola con la data attuale:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE FUNCTION &#x5B;dbo].&#x5B;Age] (@birthday DATETIME) \r\nRETURNS INT \r\nAS \r\n  BEGIN \r\n      DECLARE @age INT \r\n      DECLARE @d AS DATETIME \r\n\r\n      SET @d=GETDATE() \r\n\r\n      SELECT @age = DATEDIFF(yy, @birthday, @d) - \r\n      --Se non ha compiuto gli anni nell'ultimo anno in corso sottrae 1 anno.\r\n      ( CASE \r\n\t\t\tWHEN ( DATEPART(m,@birthday ) &gt; DATEPART(m, @d) ) \r\n              OR ( DATEPART(m, @birthday ) = DATEPART(m, @d) AND DATEPART(d, @birthday) &gt; DATEPART(d, @d) ) \r\n\t\t\tTHEN 1 \r\n            \r\n            ELSE 0 \r\n        END \r\n       ) \r\n\r\n      RETURN( @age ) \r\n  END \r\n\r\n <\/pre>\n<p>Per eseguirla :<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n\r\nSELECT DBO.AGE('05\/02\/1977')\r\n\r\n<\/pre>\n<p>Una variante potrebbe essere sapere che et\u00e0 si aveva in un anno specifico, quindi passando due parametri anzich\u00e8 uno:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE FUNCTION &#x5B;dbo].&#x5B;AgeAt] (@birthday DATETIME, @d DATETIME) \r\nRETURNS INT \r\nAS \r\n  BEGIN \r\n      DECLARE @age INT \r\n      SELECT @age = DATEDIFF(yy, @birthday, @d) - \r\n      --Se non ha compiuto gli anni nell'ultimo anno in corso sottrae 1 anno.\r\n      ( CASE \r\n\t\t\tWHEN ( DATEPART(m,@birthday ) &gt; DATEPART(m, @d) ) \r\n              OR ( DATEPART(m, @birthday ) = DATEPART(m, @d) AND DATEPART(d, @birthday) &gt; DATEPART(d, @d) ) \r\n\t\t\tTHEN 1 \r\n            \r\n            ELSE 0 \r\n        END \r\n       ) \r\n\r\n      RETURN( @age ) \r\n  END \r\n<\/pre>\n<p>E per utilizzarla semplicemente :<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nselect dbo.AgeAt ('05\/02\/1977', '01\/02\/1998') -- risultato 21\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Calcola l&#8217;et\u00e0 fornendo una data e confrontandola con la data attuale: CREATE FUNCTION &#x5B;dbo].&#x5B;Age] (@birthday DATETIME) RETURNS INT AS BEGIN DECLARE @age INT DECLARE @d AS DATETIME SET @d=GETDATE() SELECT @age = DATEDIFF(yy, @birthday, @d) &#8211; &#8211;Se non ha compiuto gli anni nell&#8217;ultimo anno in corso sottrae 1 anno. ( CASE WHEN ( DATEPART(m,@birthday ) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[29,27,3],"tags":[],"class_list":["post-361","post","type-post","status-publish","format-standard","hentry","category-coding","category-programmazione","category-sql"],"_links":{"self":[{"href":"https:\/\/www.aleprex.it\/wordpress\/wp-json\/wp\/v2\/posts\/361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aleprex.it\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aleprex.it\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aleprex.it\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aleprex.it\/wordpress\/wp-json\/wp\/v2\/comments?post=361"}],"version-history":[{"count":11,"href":"https:\/\/www.aleprex.it\/wordpress\/wp-json\/wp\/v2\/posts\/361\/revisions"}],"predecessor-version":[{"id":378,"href":"https:\/\/www.aleprex.it\/wordpress\/wp-json\/wp\/v2\/posts\/361\/revisions\/378"}],"wp:attachment":[{"href":"https:\/\/www.aleprex.it\/wordpress\/wp-json\/wp\/v2\/media?parent=361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aleprex.it\/wordpress\/wp-json\/wp\/v2\/categories?post=361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aleprex.it\/wordpress\/wp-json\/wp\/v2\/tags?post=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}