<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Le blog de SQLpro &#187; DATEDIFF</title>
	<atom:link href="https://blog.developpez.com/sqlpro/ptag/datediff/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/sqlpro</link>
	<description>Le SQL pour SQL Server, PostGreSQL et tous les autres SGBDR</description>
	<lastBuildDate>Thu, 15 Oct 2020 12:59:17 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Un DATEDIFF amélioré</title>
		<link>https://blog.developpez.com/sqlpro/p12900/ms-sql-server/un-datediff-ameliore</link>
		<comments>https://blog.developpez.com/sqlpro/p12900/ms-sql-server/un-datediff-ameliore#comments</comments>
		<pubDate>Thu, 13 Aug 2015 14:19:50 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2000]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[BIGINT]]></category>
		<category><![CDATA[calcul]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[DATEDIFF]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[différence]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[Transact SQL]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=584</guid>
		<description><![CDATA[La fonction DATEDIFF retourne un entier de type INT. Cependant cela ne suffit pas dans certaines cas : granularité de calcul trop fine (milliseconde par exemple) ou plages de valeurs trop étendues. La fonction F_DATEDIFF_BIG répond a cette problématique en vous offrant une valeur de sortie sous forme de BIGINT. Voici cette fonction. Toutes les [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>La fonction DATEDIFF retourne un entier de type INT. Cependant cela ne suffit pas dans certaines cas : granularité de calcul trop fine (milliseconde par exemple) ou plages de valeurs trop étendues. La fonction F_DATEDIFF_BIG répond a cette problématique en vous offrant une valeur de sortie sous forme de BIGINT.<br />
<span id="more-584"></span></p>
<p>Voici cette fonction. Toutes les explications et la manière de s&rsquo;en servir sont dans la cartouche.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> dbo<span style="color: #66cc66;">.</span>F_DATEDIFF_BIG <span style="color: #66cc66;">&#40;</span>@DATEPART <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @DT1 DATETIME2<span style="color: #66cc66;">,</span> @DT2 DATETIME2<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #808080; font-style: italic;">/******************************************************************************<br />
* FONCTION UDF SCALAIRE dbo.F_DATEDIFF_BIG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2015-08-13 *<br />
*******************************************************************************<br />
* Frédéric BROUARD &nbsp;- &nbsp;alias SQLpro &nbsp; - &nbsp; SARL SQL SPOT &nbsp;- &nbsp;SQLpro@outlook.fr *<br />
* Architecte de données : &nbsp;expertise, audit, conseil, formation, modélisation *<br />
* tuning, sur les SGBD Relationnels, le langage SQL, MS SQL Server/PostGreSQL *<br />
* blog: http://blog.developpez.com/sqlpro &nbsp;site: http://sqlpro.developpez.com *<br />
*******************************************************************************<br />
* Cette fonction étend la capacité de la fonction intégrée DATEDIFF lorsqu'il *<br />
* y a dépassement de capacité pour la valeur résultante. Exemple : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* SELECT DATEDIFF(minute, '0001-01-01', '9999-12-31'); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* --&gt; Msg 535, Niveau 16, État 0, Ligne 91 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp; La fonction datediff a provoqué un dépassement de capacité. Le nombre &nbsp; *<br />
* &nbsp; &nbsp; de parties de date qui séparent deux instances de date/heure est trop &nbsp; * <br />
* &nbsp; &nbsp; important. Essayez d'utiliser datediff avec une partie de date moins &nbsp; &nbsp;*<br />
* &nbsp; &nbsp; précise. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
*******************************************************************************<br />
* PARAMÈTRES EN ENTRÉE : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;@DATEPART CHAR(11) &nbsp; &nbsp; granularité de temps pour calculer la différence &nbsp;*<br />
* &nbsp; &nbsp;@DT1 &nbsp; &nbsp; &nbsp;DATETIME2 &nbsp; &nbsp;date/heure debut &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* <br />
* &nbsp; &nbsp;@DT2 &nbsp; &nbsp; &nbsp;DATETIME2 &nbsp; &nbsp;date/heure fin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* <br />
*******************************************************************************<br />
* VALEURS POSSIBLE POUR LE PARAMÈTRE @DATEPART : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;valeures acceptées &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;descriptions &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;------------------------------ &nbsp;---------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;year, &nbsp; &nbsp; &nbsp; &nbsp; yy, &nbsp;yyyy &nbsp; &nbsp; &nbsp; &nbsp; année &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;quarter, &nbsp; &nbsp; &nbsp;qq, &nbsp;q &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;trimestre &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;month, &nbsp; &nbsp; &nbsp; &nbsp;mm, &nbsp;m &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mois &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;dayofyear, &nbsp; &nbsp;dy, &nbsp;y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jour &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;day, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dd, &nbsp;d &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jour &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;week, &nbsp; &nbsp; &nbsp; &nbsp; wk, &nbsp;ww &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; semaine &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;hour, &nbsp; &nbsp; &nbsp; &nbsp; hh &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;heures &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;minute, &nbsp; &nbsp; &nbsp; mi, &nbsp;n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;minutes &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;second, &nbsp; &nbsp; &nbsp; ss, &nbsp;s &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;secondes &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;millisecond, &nbsp;ms &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;millisecondes &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
*******************************************************************************<br />
* VALEUR EN SORTIE : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;type SQL BIGINT &nbsp; &nbsp; &nbsp; &nbsp;nombre d'unité temporelles entre les deux valeurs *<br />
*******************************************************************************<br />
* EXEMPLES D'UTILISATION : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;SELECT dbo.F_DATEDIFF_BIG('minute', '0001-01-01', '9999-12-31') &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;--&gt; 5258963520 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;SELECT dbo.F_DATEDIFF_BIG('millisecond', '0001-01-01', '9999-12-31') &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;--&gt; 315537811200000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
*******************************************************************************<br />
* LIMITES : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;la granularité est limitée à la milliseconde &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;la validité du calcul est assurée pour la plage des dates ISO SQL : &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;[0001-01-01 00:00:00.0000000, 9999-12-31 23:59:59.9999999] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
******************************************************************************/</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
<span style="color: #808080; font-style: italic;">-- test de validité des paramètres</span><br />
<span style="color: #993333; font-weight: bold;">IF</span> @DT1 <span style="color: #66cc66;">&gt;</span> @DT2 &nbsp;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #993333; font-weight: bold;">NULL</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> @DATEPART <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">LOWER</span><span style="color: #66cc66;">&#40;</span>@DATEPART<span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">IF</span> @DATEPART <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'year'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'yy'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'yyyy'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'quarter'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; <span style="color: #ff0000;">'qq'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'q'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'month'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'mm'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'m'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'week'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'wk'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'ww'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'dayofyear'</span><span style="color: #66cc66;">,</span> &nbsp; <span style="color: #ff0000;">'dy'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'y'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'day'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'dd'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'d'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'hour'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'hh'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'minute'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'mi'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'n'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'second'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'ss'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'s'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'millisecond'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'ms'</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #993333; font-weight: bold;">NULL</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- sans conversion primaire :</span><br />
<span style="color: #993333; font-weight: bold;">IF</span> @DATEPART <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'year'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'yy'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'yyyy'</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp;@DT1<span style="color: #66cc66;">,</span> @DT2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">IF</span> @DATEPART <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'quarter'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; <span style="color: #ff0000;">'qq'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'q'</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>DATEDIFF<span style="color: #66cc66;">&#40;</span>QUARTER<span style="color: #66cc66;">,</span> @DT1<span style="color: #66cc66;">,</span> @DT2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">IF</span> @DATEPART <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'month'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'mm'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'m'</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> &nbsp; @DT1<span style="color: #66cc66;">,</span> @DT2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">IF</span> @DATEPART <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'week'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'wk'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'ww'</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>DATEDIFF<span style="color: #66cc66;">&#40;</span>WEEK<span style="color: #66cc66;">,</span> &nbsp; &nbsp;@DT1<span style="color: #66cc66;">,</span> @DT2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">IF</span> @DATEPART <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'dayofyear'</span><span style="color: #66cc66;">,</span> &nbsp; <span style="color: #ff0000;">'dy'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'y'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'day'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'dd'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'d'</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; @DT1<span style="color: #66cc66;">,</span> @DT2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- pour les autres cas</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- variable de sortie</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @RETVAL <span style="color: #993333; font-weight: bold;">BIGINT</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- initialisation pour le nombre de jours</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> @RETVAL <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> @DT1<span style="color: #66cc66;">,</span> @DT2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- ajout des parties d'heure si précision heure</span><br />
<span style="color: #993333; font-weight: bold;">IF</span> @DATEPART <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'hour'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'hh'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @RETVAL <span style="color: #66cc66;">=</span> @RETVAL <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">24</span> <span style="color: #66cc66;">+</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">HOUR</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>@DT1 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TIME</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>@DT2 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TIME</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span> @RETVAL;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- ajout des parties de minutes si précision minute</span><br />
<span style="color: #993333; font-weight: bold;">IF</span> @DATEPART <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'minute'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'mi'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'n'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @RETVAL <span style="color: #66cc66;">=</span> @RETVAL <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">1440</span> <span style="color: #66cc66;">+</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MINUTE</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>@DT1 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TIME</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>@DT2 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TIME</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span> @RETVAL;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- ajout des parties de seconde si précision seconde</span><br />
<span style="color: #993333; font-weight: bold;">IF</span> @DATEPART <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'second'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'ss'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'s'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @RETVAL <span style="color: #66cc66;">=</span> @RETVAL <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">86400</span> <span style="color: #66cc66;">+</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SECOND</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>@DT1 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TIME</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>@DT2 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TIME</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span> @RETVAL;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- ajout des parties de milliseconde si précision milliseconde</span><br />
<span style="color: #993333; font-weight: bold;">IF</span> @DATEPART <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'millisecond'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'ms'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @RETVAL <span style="color: #66cc66;">=</span> @RETVAL <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">86400000</span> <span style="color: #66cc66;">+</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>millisecond<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>@DT1 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TIME</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>@DT2 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TIME</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span> @RETVAL;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #993333; font-weight: bold;">NULL</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
