<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>SQL Server Journey with SQL Authority &#187; SQL Function</title>
	<atom:link href="http://blog.sqlauthority.com/category/sql-function/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:09:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.sqlauthority.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/08e35387c05b61340e885b1763a69d9f?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>SQL Server Journey with SQL Authority &#187; SQL Function</title>
		<link>http://blog.sqlauthority.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.sqlauthority.com/osd.xml" title="SQL Server Journey with SQL Authority" />
	<atom:link rel='hub' href='http://blog.sqlauthority.com/?pushpress=hub'/>
		<item>
		<title>SQL SERVER &#8211; Target Recovery Time of a Database &#8211; Advance Option in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/12/27/sql-server-target-recovery-time-of-a-database-advance-option-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/12/27/sql-server-target-recovery-time-of-a-database-advance-option-in-sql-server-2012/#comments</comments>
		<pubDate>Tue, 27 Dec 2011 01:30:11 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Backup and Restore]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16449</guid>
		<description><![CDATA[Recently I was going over few advanced options of SQL Server 2012 in database properties and I found a new option in the property screen. Properties screen of SQL Server 2008 R2 Properties screen of SQL Server 2012 I got little curious and decided to learn what does this new feature indicates. When I started [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16449&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Recently I was going over few advanced options of SQL Server 2012 in database properties and I found a new option in the property screen.</p>
<h3 style="text-align:justify;">Properties screen of SQL Server 2008 R2</h3>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/targetrecovery2008.jpg" alt="" width="500" height="505" /></p>
<h3 style="text-align:justify;">Properties screen of SQL Server 2012</h3>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/targetrecovery2012.jpg" alt="" width="500" height="504" /></p>
<p style="text-align:justify;">I got little curious and decided to learn what does this new feature indicates. When I started to learn more about this subject, I had excellent learning experience.</p>
<p style="text-align:justify;">The default value of this option is 0. This value is directly related to Checkpoint. When it is set to greater than 0 (zero) it uses indirect-checkpoints and establishes an upper-bound on recovery time for this database.</p>
<p style="text-align:justify;"><em><strong>NOTE:</strong></em> Do not attempt this right away on your production database. Make sure that you try this out on your development server and validate before making it any changes on your production server. Make sure that you do not change this option unless you are 100% sure about what you are doing and understand the risk as well implications.</p>
<p style="text-align:justify;">Indirect Checkpoints are alternative to automatic checkpoints (which should be good for most of the system and should be left alone if you are not sure about implications). If system crashes accidentally, indirect checkpoints provides potentially faster recovery time than automatic checkpoints.</p>
<p style="text-align:justify;">Reasons for enabling indirect checkpoint</p>
<ul style="text-align:justify;">
<li>It may improve database recovery time</li>
<li>It may reduce checkpoint I/O as it writes continuously pages to the disk in the background</li>
</ul>
<p style="text-align:justify;">Reasons for NOT enabling indirect checkpoint</p>
<ul style="text-align:justify;">
<li>In OLTP workload it can increase overall writes on server by writing continuously pages to the disk in the background which may reduce the performance.</li>
</ul>
<p style="text-align:justify;">I have now learned the pros and cons of this feature and I am going to test this feature out on various workload oriented servers and see what is the effect of this feature. You can read further on <a href="http://msdn.microsoft.com/en-us/library/ms189573%28v=sql.110%29.aspx?WT.mc_id=aff-n-in-loc--pd" target="_blank">MSDN </a>about this feature.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.sqlauthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-backup-and-restore/'>SQL Backup and Restore</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16449/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16449&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/12/27/sql-server-target-recovery-time-of-a-database-advance-option-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/targetrecovery2008.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/targetrecovery2012.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; A Quick Trick about SQL Server 2012 CONCAT Function &#8211; PRINT</title>
		<link>http://blog.sqlauthority.com/2011/12/21/sql-server-a-quick-trick-about-sql-server-2012-concat-function-print/</link>
		<comments>http://blog.sqlauthority.com/2011/12/21/sql-server-a-quick-trick-about-sql-server-2012-concat-function-print/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 01:30:17 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLAuthority Book Review]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16399</guid>
		<description><![CDATA[Yesterday I posted A Quick Trick about SQL Server 2012 CONCAT function and the very first comment in few minutes of Vinod Kumar. He suggested that this function should be also used with the PRINT statement as well. While I was having conversation with him &#8211; Jacob Sebastian sent me message suggesting the same. As I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16399&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Yesterday I posted <strong><a href="http://blog.sqlauthority.com/2011/12/20/sql-server-a-quick-trick-about-sql-server-2012-concat-function/" target="_blank">A Quick Trick about SQL Server 2012 CONCAT function</a></strong> and the very first comment in few minutes of <a href="http://blogs.extremeexperts.com/" target="_blank">Vinod Kumar</a>. He suggested that this function should be also used with the PRINT statement as well. While I was having conversation with him &#8211; <a href="http://beyondrelational.com" target="_blank">Jacob Sebastian</a> sent me message suggesting the same. As I got feedback in first 10 minutes of publishing the blog post &#8211; I decided to update the blog post. While I started to write there was an email from <a href="http://joes2pros.com" target="_blank">Rick Morelan</a> suggesting that this function can be used along with PRINT statement. Alright &#8211; 3 SQL experts suggesting the same thing to me in less than 15 minutes. Here is the dedicated post addressing the same idea.</p>
<p style="text-align:justify;">Here is the question how many of you had the same suggestion as Vinod, Jacob and Rick?</p>
<p style="text-align:justify;">Here is the updated version of the same article. If you use PRINT statement and directly appending string and datetime datatype you will get following error.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">PRINT </span><span style="color:red;">'Current Time ' </span><span style="color:gray;">+ </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()<br />
</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/concaterror.jpg" alt="" width="591" height="56" /></p>
<p style="text-align:justify;">The normal reaction to the same is to modify the script and convert datetime datatype to varchar datatype. This will not only remove the error but also a very common practice.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">PRINT </span><span style="color:red;">'Current Time ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">20</span><span style="color:gray;">))<br />
</span></code></p>
<p style="text-align:justify;"><em><strong>Current Time Dec 21 2011  7:00PM</strong></em></p>
<p style="text-align:justify;">Just like <strong><a href="http://blog.sqlauthority.com/2011/12/20/sql-server-a-quick-trick-about-sql-server-2012-concat-function/" target="_blank">earlier</a></strong> blog post you can now use SQL Server 2012 function CONCAT to resolve the error.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">PRINT </span><span style="color:black;">CONCAT</span><span style="color:gray;">(</span><span style="color:red;">'Current Time '</span><span style="color:gray;">, </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">())<br />
</span></code></p>
<p style="text-align:justify;">I have previously written in detail article about <strong><a href="http://blog.sqlauthority.com/2011/09/16/sql-server-denali-string-function-concat-a-quick-introduction/" target="_blank">SQL Server 2012 CONCAT function</a></strong> over here additionally, I have written quick guide on <strong><a href="http://blog.sqlauthority.com/2011/09/21/sql-server-denali-14-new-functions-a-quick-guide/" target="_blank">14 new functions of SQL Server Denali</a></strong>.</p>
<p style="text-align:justify;">I am sure Vinod, Jacob and Rick are smiling at looking at this blog post.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/sql-interview-questions-and-answers/'>SQL Interview Questions and Answers</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlauthority/sqlauthority-book-review/'>SQLAuthority Book Review</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16399/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16399/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16399/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16399/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16399/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16399/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16399/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16399/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16399&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/12/21/sql-server-a-quick-trick-about-sql-server-2012-concat-function-print/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/concaterror.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; A Quick Trick about SQL Server 2012 CONCAT function</title>
		<link>http://blog.sqlauthority.com/2011/12/20/sql-server-a-quick-trick-about-sql-server-2012-concat-function/</link>
		<comments>http://blog.sqlauthority.com/2011/12/20/sql-server-a-quick-trick-about-sql-server-2012-concat-function/#comments</comments>
		<pubDate>Tue, 20 Dec 2011 01:30:27 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLAuthority Book Review]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16376</guid>
		<description><![CDATA[Just a day before I was presenting at Virtual Tech Days and I wanted to demonstrate the current time to audience using SQL Server Management Studio, I ended up a quick error. If any of you ever tried to concat multiple values of different datatype this should not be surprise to you. SELECT 'Current Time [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16376&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Just a day before I was presenting at Virtual Tech Days and I wanted to demonstrate the current time to audience using SQL Server Management Studio, I ended up a quick error. If any of you ever tried to concat multiple values of different datatype this should not be surprise to you.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:red;">'Current Time ' </span><span style="color:gray;">+ </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()<br />
</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/concaterror.jpg" alt="" width="591" height="56" /></p>
<p style="text-align:justify;">I quickly modified script to following workaround and my script worked right away.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:red;">'Current Time ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">20</span><span style="color:gray;">))<br />
</span></code></p>
<p style="text-align:justify;"><em><strong>Current Time Dec 20 2011  7:00PM</strong></em></p>
<p style="text-align:justify;">However, I instantly realized that I can use SQL Server 2012 function which can right fit for this situation.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">CONCAT</span><span style="color:gray;">(</span><span style="color:red;">'Current Time '</span><span style="color:gray;">, </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">())<br />
</span></code></p>
<p style="text-align:justify;">I have previously written in detail article about <strong><a href="http://blog.sqlauthority.com/2011/09/16/sql-server-denali-string-function-concat-a-quick-introduction/" target="_blank">SQL Server 2012 CONCAT function</a></strong> over here additionally, I have written quick guide on <strong><a href="http://blog.sqlauthority.com/2011/09/21/sql-server-denali-14-new-functions-a-quick-guide/" target="_blank">14 new functions of SQL Server Denali</a></strong>.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/sql-interview-questions-and-answers/'>SQL Interview Questions and Answers</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlauthority/sqlauthority-book-review/'>SQLAuthority Book Review</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16376/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16376/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16376/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16376/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16376/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16376/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16376/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16376&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/12/20/sql-server-a-quick-trick-about-sql-server-2012-concat-function/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/concaterror.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Effect of SET NOCOUNT on @@ROWCOUNT</title>
		<link>http://blog.sqlauthority.com/2011/12/02/sql-server-effect-of-set-no-count-on-rowcount/</link>
		<comments>http://blog.sqlauthority.com/2011/12/02/sql-server-effect-of-set-no-count-on-rowcount/#comments</comments>
		<pubDate>Fri, 02 Dec 2011 01:30:26 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16112</guid>
		<description><![CDATA[Today I had very interesting experience when I was presenting on SQL Server. While I was presenting the session when I ran query SQL Server Management Studio returned message like (8 row(s) affected) and (2 row(s) affected) etc. After a while at one point, I started to prove usage of @@ROWCOUNT function. As I was [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16112&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Today I had very interesting experience when I was presenting on SQL Server. While I was presenting the session when I ran query SQL Server Management Studio returned message like (8 row(s) affected) and (2 row(s) affected) etc. After a while at one point, I started to prove usage of @@ROWCOUNT function.</p>
<p style="text-align:justify;">As I was in hurry, I quickly wrote very similar to following example on my screen. (The real query is much complected, but for the purpose of this post I am simplifing it here.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@table </span><span style="color:blue;">TABLE</span><span style="color:gray;">(</span><span style="color:black;">col1 </span><span style="color:blue;">INT </span><span style="color:#434343;">IDENTITY</span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:black;">1</span><span style="color:gray;">), </span><span style="color:black;">col2 </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@table</span><span style="color:gray;">(</span><span style="color:black;">col2</span><span style="color:gray;">) </span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">2932</span><span style="color:gray;">),(</span><span style="color:black;">142</span><span style="color:gray;">),(</span><span style="color:black;">352</span><span style="color:gray;">),(</span><span style="color:black;">342</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET NOCOUNT ON<br />
SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:#434343;">@table<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@@rowcount<br />
</span><span style="color:blue;">SET NOCOUNT OFF<br />
</span></code></p>
<p style="text-align:justify;">Before I execute the script nearly 10 hands went up in audience. This was bit surprise to me as I do not know why they all got alerted. I assumed that there should be something wrong with either project, screen or my display. I quickly asked to one person what is wrong, he pointed out that I will not get right results in @@ROWCOUNT as I am using immediately before SET NOCOUNT ON. When I asked the reason they told me that because any statement when ran right before @@ROWCOUNT resets its value.</p>
<p style="text-align:justify;">Let us stop here for a second &#8211; <em><strong>do YOU think same way as well?</strong></em></p>
<p style="text-align:justify;">If yes, run above code and check your result in following image.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/rowcountwithnocount.jpg" alt="" width="328" height="287" /></p>
<p style="text-align:justify;">If you believed the same I guess we learned something new. I have no problem confessing I learn something new every single time, I learn something new. However, I was bit surprised with this common myth of SET NOCOUNT ON and @@ROWCOUNT.</p>
<p style="text-align:justify;">Are there any other common myths &#8211; if yes, do share here with your comment. If I think it is interesting, I will publish it this blog with due credit and you can win my new <strong><a href="http://blog.sqlauthority.com/sql-server-books/sql-server-interview-questions-and-answers-for-all-database-developers-and-developers-administrators/" target="_blank">SQL Server Interview Questions and Answers</a></strong> book as well.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16112&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/12/02/sql-server-effect-of-set-no-count-on-rowcount/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/rowcountwithnocount.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Solution to Puzzle &#8211; Simulate LEAD() and LAG() without Using SQL Server 2012 Analytic Function</title>
		<link>http://blog.sqlauthority.com/2011/11/24/sql-server-solution-to-puzzle-simulate-lead-and-lag-without-using-sql-server-2012-analytic-function/</link>
		<comments>http://blog.sqlauthority.com/2011/11/24/sql-server-solution-to-puzzle-simulate-lead-and-lag-without-using-sql-server-2012-analytic-function/#comments</comments>
		<pubDate>Thu, 24 Nov 2011 01:30:36 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16017</guid>
		<description><![CDATA[Earlier I wrote a series on SQL Server Analytic Functions of SQL Server 2012. During the series to keep the learning maximum and having fun, we had few puzzles. One of the puzzle was simulating LEAD() and LAG() without using SQL Server 2012 Analytic Function. Please read the puzzle here first before reading the solution [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16017&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Earlier I wrote a series on <strong><a href="http://blog.sqlauthority.com/2011/11/23/sql-server-2012-summary-of-all-the-analytic-functions-msdn-and-sqlauthority/" target="_blank">SQL Server Analytic Functions of SQL Server 2012</a></strong>. During the series to keep the learning maximum and having fun, we had few puzzles. One of the puzzle was simulating LEAD() and LAG() without using SQL Server 2012 Analytic Function.</p>
<p style="text-align:justify;">Please read the puzzle here first before reading the solution : <strong><a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/" target="_blank">Write T-SQL Self Join Without Using LEAD and LAG</a></strong>. When I was originally wrote the puzzle I had done small blunder and the question was a bit confusing which I corrected later on but wrote a <strong><a href="http://blog.sqlauthority.com/2011/11/17/sql-server-puzzle-to-win-print-book-and-free-30-days-online-training-material/" target="_blank">follow up blog post</a></strong> on over here where I describe the give-away.</p>
<h2 style="text-align:justify;">Quick Recap:</h2>
<p style="text-align:justify;">Generate following results without using SQL Server 2012 analytic functions.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/leadlag1.jpg" alt="" width="514" height="262" /></p>
<p style="text-align:justify;">I had received so many valid answers. Some answers were similar to other and some were very innovative. Some answers were very adaptive and some did not work when I changed where condition. After selecting all the valid answer, I put them in table and ran RANDOM function on the same and selected winners. Here are the valid answers.</p>
<h2 style="text-align:justify;">No Joins and No Analytic Functions</h2>
<p style="text-align:justify;">Excellent Solution by <a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-197710" target="_blank">Geri Reshef</a> &#8211; Winner of <strong>SQL Server Interview Questions and Answers (<a href="http://bit.ly/indiasqlinterview" target="_blank">India </a>| <a href="http://amzn.to/qTOXoG" target="_blank">USA</a>)</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">WITH </span><span style="color:black;">T1 </span><span style="color:blue;">AS<br />
</span><span style="color:gray;">(</span><span style="color:blue;">SELECT </span><span style="color:black;">Row_Number</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">N</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.OrderQty<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">))<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">N</span><span style="color:gray;">%</span><span style="color:black;">2</span><span style="color:blue;">=</span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:magenta;">MAX</span><span style="color:gray;">(</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">N</span><span style="color:gray;">%</span><span style="color:black;">2</span><span style="color:blue;">=</span><span style="color:black;">0 </span><span style="color:blue;">THEN </span><span style="color:black;">SalesOrderDetailID </span><span style="color:blue;">END</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">Partition </span><span style="color:blue;">BY </span><span style="color:gray;">(</span><span style="color:black;">N</span><span style="color:gray;">+</span><span style="color:black;">1</span><span style="color:gray;">)/</span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:blue;">ELSE </span><span style="color:magenta;">MAX</span><span style="color:gray;">(</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">N</span><span style="color:gray;">%</span><span style="color:black;">2</span><span style="color:blue;">=</span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:black;">SalesOrderDetailID </span><span style="color:blue;">END</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">Partition </span><span style="color:blue;">BY </span><span style="color:black;">N</span><span style="color:gray;">/</span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:blue;">END </span><span style="color:black;">LeadVal</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">N</span><span style="color:gray;">%</span><span style="color:black;">2</span><span style="color:blue;">=</span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:magenta;">MAX</span><span style="color:gray;">(</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">N</span><span style="color:gray;">%</span><span style="color:black;">2</span><span style="color:blue;">=</span><span style="color:black;">0 </span><span style="color:blue;">THEN </span><span style="color:black;">SalesOrderDetailID </span><span style="color:blue;">END</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">Partition </span><span style="color:blue;">BY </span><span style="color:black;">N</span><span style="color:gray;">/</span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:blue;">ELSE </span><span style="color:magenta;">MAX</span><span style="color:gray;">(</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">N</span><span style="color:gray;">%</span><span style="color:black;">2</span><span style="color:blue;">=</span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:black;">SalesOrderDetailID </span><span style="color:blue;">END</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">Partition </span><span style="color:blue;">BY </span><span style="color:gray;">(</span><span style="color:black;">N</span><span style="color:gray;">+</span><span style="color:black;">1</span><span style="color:gray;">)/</span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:blue;">END </span><span style="color:black;">LagVal<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">T1<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">OrderQty</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<h2 style="text-align:justify;">No Analytic Function and Early Bird</h2>
<p style="text-align:justify;">Excellent Solution by<a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-196511" target="_blank"> DHall</a> &#8211; Winner of <strong><a href="http://www.pluralsight-training.net/microsoft/?utm_source=sqlauthority&amp;utm_medium=sidebar&amp;utm_content=all-the-latest&amp;utm_campaign=m1-kickoff" target="_blank">Pluralsight 30 days Subscription</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- a query to emulate LEAD() and LAG()<br />
</span><span style="color:gray;">;</span><span style="color:blue;">WITH </span><span style="color:black;">s </span><span style="color:blue;">AS </span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT<br />
</span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">ldOffset</span><span style="color:gray;">, </span><span style="color:green;">-- equiv to 2nd param of LEAD<br />
</span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">lgOffset</span><span style="color:gray;">, </span><span style="color:green;">-- equiv to 2nd param of LAG<br />
</span><span style="color:gray;">NULL </span><span style="color:blue;">AS </span><span style="color:black;">ldDefVal</span><span style="color:gray;">, </span><span style="color:green;">-- equiv to 3rd param of LEAD<br />
</span><span style="color:gray;">NULL </span><span style="color:blue;">AS </span><span style="color:black;">lgDefVal</span><span style="color:gray;">, </span><span style="color:green;">-- equiv to 3rd param of LAG<br />
</span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">row</span><span style="color:gray;">,<br />
</span><span style="color:black;">SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">OrderQty<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">( </span><span style="color:black;">sLd.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">s.ldDefVal</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">( </span><span style="color:black;">sLg.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">s.lgDefVal</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">s<br />
</span><span style="color:magenta;">LEFT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">s </span><span style="color:blue;">AS </span><span style="color:black;">sLd </span><span style="color:blue;">ON </span><span style="color:black;">s.row </span><span style="color:blue;">= </span><span style="color:black;">sLd.row </span><span style="color:gray;">- </span><span style="color:black;">s.ldOffset<br />
</span><span style="color:magenta;">LEFT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">s </span><span style="color:blue;">AS </span><span style="color:black;">sLg </span><span style="color:blue;">ON </span><span style="color:black;">s.row </span><span style="color:blue;">= </span><span style="color:black;">sLg.row </span><span style="color:gray;">+ </span><span style="color:black;">s.lgOffset<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">s.OrderQty</span></code></p>
<h2 style="text-align:justify;">No Analytic Function and Partition By</h2>
<p style="text-align:justify;">Excellent Solution by<a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-196511" target="_blank"> DHall</a> &#8211; Winner of <strong><a href="http://www.pluralsight-training.net/microsoft/?utm_source=sqlauthority&amp;utm_medium=sidebar&amp;utm_content=all-the-latest&amp;utm_campaign=m1-kickoff" target="_blank">Pluralsight 30 days Subscription</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* a query to emulate LEAD() and LAG() */<br />
</span><span style="color:gray;">;</span><span style="color:blue;">WITH </span><span style="color:black;">s </span><span style="color:blue;">AS </span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT<br />
</span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">LeadOffset</span><span style="color:gray;">, </span><span style="color:green;">/* equiv to 2nd param of LEAD */<br />
</span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">LagOffset</span><span style="color:gray;">, </span><span style="color:green;">/* equiv to 2nd param of LAG */<br />
</span><span style="color:gray;">NULL </span><span style="color:blue;">AS </span><span style="color:black;">LeadDefVal</span><span style="color:gray;">, </span><span style="color:green;">/* equiv to 3rd param of LEAD */<br />
</span><span style="color:gray;">NULL </span><span style="color:blue;">AS </span><span style="color:black;">LagDefVal</span><span style="color:gray;">, </span><span style="color:green;">/* equiv to 3rd param of LAG */<br />
/* Try changing the values of the 4 integer values above to see their effect on the results */<br />
/* The values given above of 0, 0, null and null<br />
behave the same as the default 2nd and 3rd parameters to LEAD() and LAG() */<br />
</span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">row</span><span style="color:gray;">,<br />
</span><span style="color:black;">SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">OrderQty<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">( </span><span style="color:black;">sLead.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">s.LeadDefVal</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">( </span><span style="color:black;">sLag.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">s.LagDefVal</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">s<br />
</span><span style="color:magenta;">LEFT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">s </span><span style="color:blue;">AS </span><span style="color:black;">sLead<br />
</span><span style="color:blue;">ON </span><span style="color:black;">s.row </span><span style="color:blue;">= </span><span style="color:black;">sLead.row </span><span style="color:gray;">- </span><span style="color:black;">s.LeadOffset<br />
</span><span style="color:green;">/* Try commenting out this next line when LeadOffset != 0 */<br />
</span><span style="color:gray;">AND </span><span style="color:black;">s.SalesOrderID </span><span style="color:blue;">= </span><span style="color:black;">sLead.SalesOrderID<br />
</span><span style="color:green;">/* The additional join criteria on SalesOrderID above<br />
is equivalent to PARTITION BY SalesOrderID<br />
in the OVER clause of the LEAD() function */<br />
</span><span style="color:magenta;">LEFT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">s </span><span style="color:blue;">AS </span><span style="color:black;">sLag<br />
</span><span style="color:blue;">ON </span><span style="color:black;">s.row </span><span style="color:blue;">= </span><span style="color:black;">sLag.row </span><span style="color:gray;">+ </span><span style="color:black;">s.LagOffset<br />
</span><span style="color:green;">/* Try commenting out this next line when LagOffset != 0 */<br />
</span><span style="color:gray;">AND </span><span style="color:black;">s.SalesOrderID </span><span style="color:blue;">= </span><span style="color:black;">sLag.SalesOrderID<br />
</span><span style="color:green;">/* The additional join criteria on SalesOrderID above<br />
is equivalent to PARTITION BY SalesOrderID<br />
in the OVER clause of the LAG() function */<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">s.OrderQty</span></code></p>
<h2 style="text-align:justify;">No Analytic Function and CTE Usage</h2>
<p style="text-align:justify;">Excellent Solution by <a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-196633" target="_blank">Pravin Patel</a> <strong>- </strong>Winner of<strong> SQL Server Interview Questions and Answers (<a href="http://bit.ly/indiasqlinterview" target="_blank">India </a>| <a href="http://amzn.to/qTOXoG" target="_blank">USA</a>)</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">--CTE based solution<br />
</span><span style="color:gray;">;<br />
</span><span style="color:blue;">WITH </span><span style="color:black;">cteMain<br />
</span><span style="color:blue;">AS<br />
</span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT<br />
</span><span style="color:black;">SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">sn<br />
</span><span style="color:blue;">FROM<br />
</span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE<br />
</span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
)<br />
</span><span style="color:blue;">SELECT<br />
</span><span style="color:black;">m.SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">m.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">m.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">sLead.SalesOrderDetailID </span><span style="color:blue;">AS </span><span style="color:black;">leadvalue</span><span style="color:gray;">,<br />
</span><span style="color:black;">sLeg.SalesOrderDetailID </span><span style="color:blue;">AS </span><span style="color:black;">leagvalue<br />
</span><span style="color:blue;">FROM<br />
</span><span style="color:black;">cteMain </span><span style="color:blue;">AS </span><span style="color:black;">m<br />
</span><span style="color:magenta;">LEFT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">cteMain </span><span style="color:blue;">AS </span><span style="color:black;">sLead<br />
</span><span style="color:blue;">ON </span><span style="color:black;">sLead.sn </span><span style="color:blue;">= </span><span style="color:black;">m.sn</span><span style="color:gray;">+</span><span style="color:black;">1<br />
</span><span style="color:magenta;">LEFT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">cteMain </span><span style="color:blue;">AS </span><span style="color:black;">sLeg<br />
</span><span style="color:blue;">ON </span><span style="color:black;">sLeg.sn </span><span style="color:blue;">= </span><span style="color:black;">m.sn</span><span style="color:gray;">-</span><span style="color:black;">1<br />
</span><span style="color:blue;">ORDER BY<br />
</span><span style="color:black;">m.SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">m.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">m.OrderQty </span></code></p>
<h2 style="text-align:justify;">No Analytic Function and Co-Related Subquery Usage</h2>
<p style="text-align:justify;">Excellent Solution by <a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-196633" target="_blank">Pravin Patel</a> &#8211; Winner of<strong> SQL Server Interview Questions and Answers (<a href="http://bit.ly/indiasqlinterview" target="_blank">India </a>| <a href="http://amzn.to/qTOXoG" target="_blank">USA</a>)</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Co-Related subquery<br />
</span><span style="color:blue;">SELECT<br />
</span><span style="color:black;">m.SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">m.SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">m.OrderQty</span><span style="color:gray;">,<br />
( </span><span style="color:blue;">SELECT </span><span style="color:magenta;">MIN</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail </span><span style="color:blue;">AS </span><span style="color:black;">l<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">l.SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
AND </span><span style="color:black;">l.SalesOrderID </span><span style="color:gray;">&gt;= </span><span style="color:black;">m.SalesOrderID </span><span style="color:gray;">AND </span><span style="color:black;">l.SalesOrderDetailID </span><span style="color:gray;">&gt; </span><span style="color:black;">m.SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">lead</span><span style="color:gray;">,<br />
( </span><span style="color:blue;">SELECT </span><span style="color:magenta;">MAX</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail </span><span style="color:blue;">AS </span><span style="color:black;">l<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">l.SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
AND </span><span style="color:black;">l.SalesOrderID </span><span style="color:gray;">&lt;= </span><span style="color:black;">m.SalesOrderID </span><span style="color:gray;">AND </span><span style="color:black;">l.SalesOrderDetailID </span><span style="color:gray;">&lt; </span><span style="color:black;">m.SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">leag<br />
</span><span style="color:blue;">FROM<br />
</span><span style="color:black;">Sales.SalesOrderDetail </span><span style="color:blue;">AS </span><span style="color:black;">m<br />
</span><span style="color:blue;">WHERE<br />
</span><span style="color:black;">m.SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY<br />
</span><span style="color:black;">m.SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">m.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">m.OrderQty </span></code></p>
<p style="text-align:justify;">This was one of the most interesting Puzzle on this blog.</p>
<h2 style="text-align:justify;">Giveaway</h2>
<p style="text-align:justify;">Winners will get following giveaways.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-197710" target="_blank">Geri Reshef</a> and <a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-196633" target="_blank">Pravin Patel</a></p>
<p style="text-align:justify;"><strong>SQL Server Interview Questions and Answers (<a href="http://bit.ly/indiasqlinterview" target="_blank">India </a>| <a href="http://amzn.to/qTOXoG" target="_blank">USA</a>)</strong></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-196511" target="_blank"> DHall</a></p>
<p style="text-align:justify;"><strong><a href="http://www.pluralsight-training.net/microsoft/?utm_source=sqlauthority&amp;utm_medium=sidebar&amp;utm_content=all-the-latest&amp;utm_campaign=m1-kickoff" target="_blank">Pluralsight 30 days Subscription</a></strong></p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/readers-contribution/'>Readers Contribution</a>, <a href='http://blog.sqlauthority.com/category/readers-question/'>Readers Question</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/sql-puzzle/'>SQL Puzzle</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16017/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16017/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16017/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16017/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16017/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16017/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16017/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16017&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/24/sql-server-solution-to-puzzle-simulate-lead-and-lag-without-using-sql-server-2012-analytic-function/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/leadlag1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; 2012 &#8211; Summary of All the Analytic Functions &#8211; MSDN and SQLAuthority</title>
		<link>http://blog.sqlauthority.com/2011/11/23/sql-server-2012-summary-of-all-the-analytic-functions-msdn-and-sqlauthority/</link>
		<comments>http://blog.sqlauthority.com/2011/11/23/sql-server-2012-summary-of-all-the-analytic-functions-msdn-and-sqlauthority/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 01:30:26 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15998</guid>
		<description><![CDATA[SQL Server 2012 (RC0 Available here) has introduced new analytic functions. These functions were long awaited and I am glad that they are now here. Before when any of this function was needed, people used to write long T-SQL code to simulate these functions. But now there’s no need of doing so. Having available native [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15998&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">SQL Server 2012 (<a href="http://blog.sqlauthority.com/2011/11/19/sql-server-2012-rc0-various-resources-and-downloads/" target="_blank"><strong>RC0 Available here</strong></a>) has introduced new analytic functions. These functions were long awaited and I am glad that they are now here. Before when any of this function was needed, people used to write long T-SQL code to simulate these functions. But now there’s no need of doing so. Having available native function also helps performance as well readability.</p>
<p style="text-align:justify;">In the last few days I have written many articles on this subject on my blog. The goal was to make these complex analytic functions easy to understand and make them widely accepted. As these new functions are available and as awareness spreads about them, we should start using these new functions. Here is a quick list of the new functions and relevant MSDN sites:</p>
<table border="1">
<tbody>
<tr>
<td><strong>Function</strong></td>
<td><strong>SQLAuthority</strong></td>
<td><strong>MSDN</strong></td>
</tr>
<tr>
<td>CUME_DIST</td>
<td><strong><a href="http://blog.sqlauthority.com/2011/11/08/sql-server-introduction-to-cume_dist-analytic-functions-introduced-in-sql-server-2012/" target="_blank">CUME_DIST</a></strong></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh231078(v=SQL.110).aspx?ocid=aff-n-in-loc--pd" target="_blank">CUME_DIST</a></td>
</tr>
<tr>
<td>FIRST_VALUE</td>
<td><strong><a href="http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/" target="_blank">FIRST_VALUE</a></strong></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213018(v=SQL.110).aspx?ocid=aff-n-in-loc--pd" target="_blank">FIRST_VALUE</a></td>
</tr>
<tr>
<td>LAST_VALUE</td>
<td><strong><a href="http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/" target="_blank">LAST_VALUE</a></strong></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh231517(v=SQL.110).aspx?ocid=aff-n-in-loc--pd" target="_blank">LAST_VALUE</a></td>
</tr>
<tr>
<td>LEAD</td>
<td><strong><a href="http://blog.sqlauthority.com/2011/11/15/sql-server-introduction-to-lead-and-lag-analytic-functions-introduced-in-sql-server-2012/" target="_blank">LEAD</a></strong></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213125(v=SQL.110).aspx?ocid=aff-n-in-loc--pd" target="_blank">LEAD</a></td>
</tr>
<tr>
<td>LAG</td>
<td><strong><a href="http://blog.sqlauthority.com/2011/11/15/sql-server-introduction-to-lead-and-lag-analytic-functions-introduced-in-sql-server-2012/" target="_blank">LAG</a></strong></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh231256(v=SQL.110).aspx?ocid=aff-n-in-loc--pd" target="_blank">LAG</a></td>
</tr>
<tr>
<td>PERCENTILE_CONT</td>
<td><strong><a href="http://blog.sqlauthority.com/2011/11/20/sql-server-introduction-to-percentile_cont-analytic-functions-introduced-in-sql-server-2012/" target="_blank">PERCENTILE_CONT</a></strong></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh231473(v=SQL.110).aspx?ocid=aff-n-in-loc--pd" target="_blank">PERCENTILE_CONT</a></td>
</tr>
<tr>
<td>PERCENTILE_DISC</td>
<td><strong><a href="http://blog.sqlauthority.com/2011/11/22/sql-server-introduction-to-percentile_disc-analytic-functions-introduced-in-sql-server-2012/" target="_blank">PERCENTILE_DISC</a></strong></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh231327(v=SQL.110).aspx?ocid=aff-n-in-loc--pd" target="_blank">PERCENTILE_DISC</a></td>
</tr>
<tr>
<td>PERCENT_RANK</td>
<td><strong><a href="http://blog.sqlauthority.com/2011/11/18/sql-server-introduction-to-percent_rank-analytic-functions-introduced-in-sql-server-2012/" target="_blank">PERCENT_RANK</a></strong></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213573(v=SQL.110).aspx?ocid=aff-n-in-loc--pd" target="_blank">PERCENT_RANK</a></td>
</tr>
</tbody>
</table>
<p style="text-align:justify;">I also enjoyed three different puzzles during the course of this series which gave a clear idea to the SQL Server 2012 analytic functions.</p>
<ul style="text-align:justify;">
<li><strong><a title="Permanent Link to SQL SERVER – Puzzle to Win Print Book – Functions FIRST_VALUE and LAST_VALUE with OVER clause and ORDER BY" href="http://blog.sqlauthority.com/2011/11/11/sql-server-puzzle-to-win-print-book-functions-first_value-and-last_value-with-over-clause-and-order-by/" rel="bookmark">SQL SERVER – Puzzle to Win Print Book – Functions FIRST_VALUE and LAST_VALUE with OVER clause and ORDER BY</a></strong></li>
<li><strong><a title="Permanent Link to SQL SERVER – Puzzle to Win Print Book – Write T-SQL Self Join Without Using LEAD and LAG" href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/" rel="bookmark">SQL SERVER – Puzzle to Win Print Book – Write T-SQL Self Join Without Using LEAD and LAG</a></strong></li>
<li><strong><a title="Permanent Link to SQL SERVER – Puzzle to Win Print Book – Explain Value of PERCENTILE_CONT() Using Simple Example" href="http://blog.sqlauthority.com/2011/11/21/sql-server-puzzle-to-win-print-book-explain-value-of-percentile_cont-using-simple-example/" rel="bookmark">SQL SERVER – Puzzle to Win Print Book – Explain Value of PERCENTILE_CONT() Using Simple Example</a></strong></li>
</ul>
<p style="text-align:justify;">This series will always be my dear series as during this series I had went through a very unique experience of <a href="http://blog.sqlauthority.com/2011/11/14/sqlauthority-news-a-real-story-of-book-getting-out-of-stock-to-a-25-discount-story-available/" target="_blank"><strong>my book going out of stock and becoming available after 48 hours</strong></a>.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>) </strong><strong><br />
</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/codeproject-2/'>CodeProject</a>, <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15998/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15998/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15998/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15998/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15998/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15998/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15998/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15998/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15998/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15998/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15998/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15998/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15998/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15998/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15998&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/23/sql-server-2012-summary-of-all-the-analytic-functions-msdn-and-sqlauthority/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to PERCENTILE_DISC()  &#8211; Analytic Functions Introduced in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/11/22/sql-server-introduction-to-percentile_disc-analytic-functions-introduced-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/11/22/sql-server-introduction-to-percentile_disc-analytic-functions-introduced-in-sql-server-2012/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 01:30:03 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15977</guid>
		<description><![CDATA[SQL Server 2012 introduces new analytical function PERCENTILE_DISC(). The book online gives following definition of this function: Computes a specific percentile for sorted values in an entire rowset or within distinct partitions of a rowset in Microsoft SQL Server 2012 Release Candidate 0 (RC 0). For a given percentile value P, PERCENTILE_DISC sorts the values of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15977&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">SQL Server 2012 introduces new analytical function PERCENTILE_DISC().</p>
<p style="text-align:justify;">The book online gives following definition of this function: Computes a specific percentile for sorted values in an entire rowset or within distinct partitions of a rowset in Microsoft SQL Server 2012 Release Candidate 0 (RC 0). For a given percentile value P, PERCENTILE_DISC sorts the values of the expression in the ORDER BY clause and returns the value with the smallest CUME_DIST value (with respect to the same sort specification) that is greater than or equal to P.</p>
<p style="text-align:justify;">If you are clear with understanding of the function &#8211; no need to read further. If you got lost here is the same in simple words &#8211; find value of the column which is equal or more than CUME_DIST.</p>
<p style="text-align:justify;">Before you continue reading this blog I strongly suggest you read about CUME_DIST function over here <strong><a title="Permanent Link to SQL SERVER – Introduction to CUME_DIST – Analytic Functions Introduced in SQL Server 2012" href="http://blog.sqlauthority.com/2011/11/08/sql-server-introduction-to-cume_dist-analytic-functions-introduced-in-sql-server-2012/" rel="bookmark">Introduction to CUME_DIST – Analytic Functions Introduced in SQL Server 2012</a></strong>.</p>
<p style="text-align:justify;">Now let’s have fun following query:</p>
<p><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">CUME_DIST</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">CDist</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENTILE_DISC</span><span style="color:gray;">(</span><span style="color:black;">0.5</span><span style="color:gray;">) </span><span style="color:black;">WITHIN </span><span style="color:blue;">GROUP </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">PercentileDisc<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/percentiledisc11.jpg" alt="" width="499" height="272" /></p>
<p style="text-align:justify;">You can see that I have used PERCENTILE_DISC(0.5) in query, which is similar to finding median but not exactly. PERCENTILE_DISC() function takes a percentile as a passing parameters. It returns the value as answer which value is equal or great to the percentile value which is passed into the example. For example in above example we are passing 0.5 into the PERCENTILE_DISC() function. It will go through the resultset and identify which rows has values which are equal to or great than 0.5. In first example it found two rows which are equal to 0.5 and the value of ProductID of that row is the answer of PERCENTILE_DISC(). In some third windowed resultset there is only single row with the CUME_DIST() value as 1 and that is for sure higher than 0.5 making it as a answer.</p>
<p style="text-align:justify;">To make sure that we are clear with this example properly. Here is one more example where I am passing 0.6 as a percentile.</p>
<p>Now let’s have fun following query:<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">CUME_DIST</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">CDist</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENTILE_DISC</span><span style="color:gray;">(</span><span style="color:black;">0.6</span><span style="color:gray;">) </span><span style="color:black;">WITHIN </span><span style="color:blue;">GROUP </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">PercentileDisc<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p>The above query will give us the following result:</p>
<p><img class="alignnone" src="http://www.pinaldave.com/bimg/percentiledisc12.jpg" alt="" width="500" height="280" /></p>
<p style="text-align:justify;">The result of the PERCENTILE_DISC(0.6) is ProductID of which CUME_DIST() is more than 0.6. This means for SalesOrderID 43670 has row with CUME_DIST() 0.75 is the qualified row, resulting answer 773 for ProductID.</p>
<p style="text-align:justify;">I hope this explanation makes it further clear.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15977/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15977/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15977/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15977/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15977/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15977/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15977/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15977&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/22/sql-server-introduction-to-percentile_disc-analytic-functions-introduced-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/percentiledisc11.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/percentiledisc12.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Puzzle to Win Print Book &#8211; Explain Value of PERCENTILE_CONT() Using Simple Example</title>
		<link>http://blog.sqlauthority.com/2011/11/21/sql-server-puzzle-to-win-print-book-explain-value-of-percentile_cont-using-simple-example/</link>
		<comments>http://blog.sqlauthority.com/2011/11/21/sql-server-puzzle-to-win-print-book-explain-value-of-percentile_cont-using-simple-example/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 01:30:00 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15968</guid>
		<description><![CDATA[From last several days I am working on various Denali Analytical functions and it is indeed really fun to refresh the concept which I studied in the school. Earlier I wrote article where I explained how we can use PERCENTILE_CONT() to find median over here SQL SERVER – Introduction to PERCENTILE_CONT() – Analytic Functions Introduced in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15968&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">From last several days I am working on various Denali Analytical functions and it is indeed really fun to refresh the concept which I studied in the school.</p>
<p style="text-align:justify;">Earlier I wrote article where I explained how we can use PERCENTILE_CONT() to find median over here <strong><a title="Permanent Link to SQL SERVER – Introduction to PERCENTILE_CONT()  – Analytic Functions Introduced in SQL Server 2012" href="http://blog.sqlauthority.com/2011/11/20/sql-server-introduction-to-percentile_cont-analytic-functions-introduced-in-sql-server-2012/" rel="bookmark">SQL SERVER – Introduction to PERCENTILE_CONT() – Analytic Functions Introduced in SQL Server 2012</a></strong>. Today I am going to ask question based on the same blog post. Again just like last time the intention of this puzzle is as following:</p>
<ol style="text-align:justify;">
<li>Learn new concept of SQL Server 2012</li>
<li>Learn new concept of SQL Server 2012 even if you are on earlier version of SQL Server.</li>
</ol>
<p style="text-align:justify;">On another note, SQL Server 2012 RC0 has been announced and available to download <strong><a title="Permanent Link to SQL SERVER – 2012 RC0 Various Resources and Downloads" href="http://blog.sqlauthority.com/2011/11/19/sql-server-2012-rc0-various-resources-and-downloads/" rel="bookmark">SQL SERVER – 2012 RC0 Various Resources and Downloads</a></strong>.</p>
<p style="text-align:justify;">Now let’s have fun following query:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENTILE_CONT</span><span style="color:gray;">(</span><span style="color:black;">0.5</span><span style="color:gray;">) </span><span style="color:black;">WITHIN </span><span style="color:blue;">GROUP </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">MedianCont<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/percentiledisc1.jpg" alt="" width="514" height="262" /></p>
<p style="text-align:justify;">The reason we get median is because we are passing value .05 to PERCENTILE_COUNT() function. Now run read the puzzle.</p>
<h2 style="text-align:justify;"><strong>Puzzle: </strong></h2>
<p style="text-align:justify;">Run following T-SQL code:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENTILE_CONT</span><span style="color:gray;">(</span><span style="color:black;">0.9</span><span style="color:gray;">) </span><span style="color:black;">WITHIN </span><span style="color:blue;">GROUP </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">MedianCont<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code><br />
Observe the result and you will notice that MidianCont has different value than before, the reason is PERCENTILE_CONT function has 0.9 value passed. For first four value the value is 775.1.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/median1.jpg" alt="" width="499" height="259" /></p>
<p style="text-align:justify;">Now run following T-SQL code:<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENTILE_CONT</span><span style="color:gray;">(</span><span style="color:black;">0.1</span><span style="color:gray;">) </span><span style="color:black;">WITHIN </span><span style="color:blue;">GROUP </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">MedianCont<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">Observe the result and you will notice that MidianCont has different value than before, the reason is PERCENTILE_CONT function has 0.1 value passed. For first four value the value is 709.3.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/median2.jpg" alt="" width="501" height="255" /></p>
<p style="text-align:justify;">Now in my example I have explained how the median is found using this function. You have to explain using mathematics and explain (in easy words) why the value in last columns are 709.3 and 775.1</p>
<h2 style="text-align:justify;">Hint:</h2>
<ul style="text-align:justify;">
<li><strong><a href="http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/" target="_blank">SQL SERVER – Introduction to PERCENTILE_CONT() – Analytic Functions Introduced in SQL Server 2012</a></strong></li>
</ul>
<h2 style="text-align:justify;">Rules</h2>
<ul style="text-align:justify;">
<li>Leave a comment with your detailed answer by Nov 25′s blog post.</li>
<li>Open world-wide (where Amazon ships books)</li>
<li>If you blog about puzzle’s solution and if you win, you win additional surprise gift as well.</li>
</ul>
<h2 style="text-align:justify;">Prizes</h2>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/sql-server-books/sql-server-interview-questions-and-answers-for-all-database-developers-and-developers-administrators/" target="_blank"><img src="http://www.pinaldave.com/bimg/sqlinterview_s" alt="" width="169" height="258" /></a></p>
<p style="text-align:justify;">Print copy of my new book SQL Server Interview Questions <strong><a href="http://amzn.to/qTOXoG" target="_blank">Amazon</a>|<a href="http://bit.ly/indiasqlinterview" target="_blank">Flipkart</a></strong></p>
<p style="text-align:justify;">If you already have this book, you can opt for any of my other books SQL Wait Stats [<strong><a href="http://amzn.to/p3tL1n" target="_blank">Amazon</a>|<a href="http://amzn.to/waitstatskindle" target="_blank">Flipkart</a>|<a href="http://amzn.to/waitstatskindle" target="_blank">Kindle</a></strong>] and SQL Programming [<strong><a href="http://amzn.to/rsC2La" target="_blank">Amazon</a>|<a href="http://bit.ly/pinalbook4" target="_blank">Flipkart</a>|<a href="http://amzn.to/sqlbook4onkindle" target="_blank">Kindle</a></strong>].</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/sql-puzzle/'>SQL Puzzle</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15968/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15968/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15968/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15968/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15968/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15968/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15968/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15968&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/21/sql-server-puzzle-to-win-print-book-explain-value-of-percentile_cont-using-simple-example/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/percentiledisc1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/median1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/median2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/sqlinterview_s" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to PERCENTILE_CONT()  &#8211; Analytic Functions Introduced in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/11/20/sql-server-introduction-to-percentile_cont-analytic-functions-introduced-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/11/20/sql-server-introduction-to-percentile_cont-analytic-functions-introduced-in-sql-server-2012/#comments</comments>
		<pubDate>Sun, 20 Nov 2011 01:30:01 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15960</guid>
		<description><![CDATA[SQL Server 2012 introduces new analytical function PERCENTILE_CONT(). The book online gives following definition of this function: Calculates a percentile based on a continuous distribution of the column value in Microsoft SQL Server 2012 Release Candidate 0 (RC 0). The result is interpolated and might not be equal to any of the specific values in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15960&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">SQL Server 2012 introduces new analytical function PERCENTILE_CONT().</p>
<p style="text-align:justify;">The book online gives following definition of this function: Calculates a percentile based on a continuous distribution of the column value in Microsoft SQL Server 2012 Release Candidate 0 (RC 0). The result is interpolated and might not be equal to any of the specific values in the column.</p>
<p style="text-align:justify;">If you are clear with understanding of the function &#8211; no need to read further. If you got lost here is the same in simple words &#8211; it is lot like finding median with percentile value.</p>
<p style="text-align:justify;">Now let’s have fun following query:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENTILE_CONT</span><span style="color:gray;">(</span><span style="color:black;">0.5</span><span style="color:gray;">) </span><span style="color:black;">WITHIN </span><span style="color:blue;">GROUP </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">MedianCont<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/percentiledisc2.jpg" alt="" width="500" height="265" /></p>
<p style="text-align:justify;">You can see that I have used PERCENTILE_COUNT(0.5) in query, which is similar to finding median. Let me explain above diagram with little more explanation. The defination of median is as following:</p>
<p style="text-align:justify;">In case of Even Number of elements = In ordered list add the two digits from the middle and devide by 2<br />
In case of Odd Numbers of elements = In ordered list select the digits from the middle</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/percentiledisc1.jpg" alt="" width="500" height="265" /></p>
<p style="text-align:justify;">I hope this example gives clear idea how PERCENTILE_CONT() works.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15960/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15960&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/20/sql-server-introduction-to-percentile_cont-analytic-functions-introduced-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/percentiledisc2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/percentiledisc1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to PERCENT_RANK() &#8211; Analytic Functions Introduced in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/11/18/sql-server-introduction-to-percent_rank-analytic-functions-introduced-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/11/18/sql-server-introduction-to-percent_rank-analytic-functions-introduced-in-sql-server-2012/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 01:30:44 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15944</guid>
		<description><![CDATA[SQL Server 2012 introduces new analytical functions PERCENT_RANK(). This function returns relative standing of a value within a query result set or partition. It will be very difficult to explain this in words so I’d like to attempt to explain its function through a brief example. Instead of creating a new table, I will be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15944&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">SQL Server 2012 introduces new analytical functions PERCENT_RANK(). This function returns relative standing of a value within a query result set or partition. It will be very difficult to explain this in words so I’d like to attempt to explain its function through a brief example. Instead of creating a new table, I will be using the AdventureWorks sample database as most developers use that for experiment purposes.</p>
<p style="text-align:justify;">Now let’s have fun following query:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">RANK</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:black;">Rnk</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENT_RANK</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">PctDist<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">PctDist </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/percentrank.jpg" alt="" width="500" height="390" /></p>
<p style="text-align:justify;">Now let us understand the resultset. You will notice that I have also included the RANK() function along with this query. The reason to include RANK() function was as this query is infect uses RANK function and find the relative standing of the query.</p>
<p style="text-align:justify;">The formula to find PERCENT_RANK() is as following:</p>
<p style="text-align:justify;"><strong>PERCENT_RANK() = (RANK() – 1) / (Total Rows – 1)</strong></p>
<p style="text-align:justify;">If you want to read more about this function read <a href="http://en.wikipedia.org/wiki/Percentile" target="_blank">here</a>.</p>
<p style="text-align:justify;">Now let us attempt the same example with PARTITION BY clause</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">RANK</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID </span><span style="color:gray;">) </span><span style="color:black;">Rnk</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENT_RANK</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">PctDist<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">PctDist </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">Now you will notice that the same logic is followed in follow result set.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/percentrank1.jpg" alt="" width="501" height="284" /></p>
<p style="text-align:justify;">I have now quick question to you &#8211; how many of you know the logic/formula of PERCENT_RANK() before this blog post?</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15944/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15944/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15944/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15944/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15944/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15944/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15944/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15944&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/18/sql-server-introduction-to-percent_rank-analytic-functions-introduced-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/percentrank.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/percentrank1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Puzzle to Win Print Book &#8211; Write T-SQL Self Join Without Using LEAD and LAG</title>
		<link>http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/</link>
		<comments>http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 01:30:23 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15845</guid>
		<description><![CDATA[Last week we asked a puzzle SQL SERVER – Puzzle to Win Print Book – Functions FIRST_VALUE and LAST_VALUE with OVER clause and ORDER BY . This puzzle got very interesting participation. The details of the winner is listed here. In this puzzle we received two very important feedback. This puzzle cleared the concepts of First_Value [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15845&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Last week we asked a puzzle <strong><a title="Permanent Link to SQL SERVER – Puzzle to Win Print Book – Functions FIRST_VALUE and LAST_VALUE with OVER clause and ORDER BY" href="http://blog.sqlauthority.com/2011/11/11/sql-server-puzzle-to-win-print-book-functions-first_value-and-last_value-with-over-clause-and-order-by/" rel="bookmark">SQL SERVER – Puzzle to Win Print Book – Functions FIRST_VALUE and LAST_VALUE with OVER clause and ORDER BY</a></strong> . This puzzle got very interesting participation. The details of the winner is listed <strong><a href="http://blog.sqlauthority.com/2011/11/11/sql-server-puzzle-to-win-print-book-functions-first_value-and-last_value-with-over-clause-and-order-by/#comment-196163" target="_blank">here</a></strong>.</p>
<p style="text-align:justify;">In this puzzle we received two very important feedback.</p>
<ol style="text-align:justify;">
<li>This puzzle cleared the concepts of First_Value and Last_Value to the participants.</li>
<li>As this was based on SQL Server 2012 many could not participate it as they have yet not installed SQL Server 2012.</li>
</ol>
<p style="text-align:justify;">I really appreciate the feedback of user and decided to come up something as fun and helps learn new feature of SQL Server 2012.</p>
<p style="text-align:justify;">Please read yesterday&#8217;s blog post <strong><a title="Permanent Link to SQL SERVER – Introduction to LEAD and LAG – Analytic Functions Introduced in SQL Server 2012" href="http://blog.sqlauthority.com/2011/11/15/sql-server-introduction-to-lead-and-lag-analytic-functions-introduced-in-sql-server-2012/" rel="bookmark">SQL SERVER – Introduction to LEAD and LAG – Analytic Functions Introduced in SQL Server 2012 </a></strong>before continuing this puzzle as it is based on yesterday&#8217;s post.</p>
<p style="text-align:justify;">Let us fun following query.<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">LEAD</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAG</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">Above query will give us following result.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/leadlag1.jpg" alt="" width="500" /></p>
<h2 style="text-align:justify;"><strong>Puzzle: </strong></h2>
<p style="text-align:justify;">Now use T-SQL Self Join where same table is joined to itself and get the same result without using LEAD or LAG functions.</p>
<h2 style="text-align:justify;">Hint:</h2>
<ul style="text-align:justify;">
<li><strong><a href="http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/" target="_blank">Introduction to JOINs – Basic of JOINs</a></strong></li>
<li><strong><a href="http://blog.sqlauthority.com/2010/07/08/sql-server-the-self-join-inner-join-and-outer-join/" target="_blank">Self Join</a></strong></li>
<li><strong><a href="http://beyondrelational.com/justlearned/posts/431/a-new-analytic-functions-in-sql-server-denali-ctp3-lead-and-lag.aspx" target="_blank">A new analytic functions in SQL Server Denali CTP3 &#8211; LEAD() and LAG()</a></strong></li>
</ul>
<h2 style="text-align:justify;">Rules</h2>
<ul style="text-align:justify;">
<li>Leave a comment with your detailed answer by Nov 21′s blog post.</li>
<li>Open world-wide (where Amazon ships books)</li>
<li>If you blog about puzzle’s solution and if you win, you win additional surprise gift as well.</li>
</ul>
<h2 style="text-align:justify;">Prizes</h2>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/sql-server-books/sql-server-interview-questions-and-answers-for-all-database-developers-and-developers-administrators/" target="_blank"><img src="http://www.pinaldave.com/bimg/sqlinterview_s" alt="" width="169" height="258" /></a></p>
<p style="text-align:justify;">Print copy of my new book SQL Server Interview Questions <strong><a href="http://amzn.to/qTOXoG" target="_blank">Amazon</a>|<a href="http://bit.ly/indiasqlinterview" target="_blank">Flipkart</a></strong></p>
<p style="text-align:justify;">If you already have this book, you can opt for any of my other books SQL Wait Stats [<strong><a href="http://amzn.to/p3tL1n" target="_blank">Amazon</a>|<a href="http://amzn.to/waitstatskindle" target="_blank">Flipkart</a>|<a href="http://amzn.to/waitstatskindle" target="_blank">Kindle</a></strong>] and SQL Programming [<strong><a href="http://amzn.to/rsC2La" target="_blank">Amazon</a>|<a href="http://bit.ly/pinalbook4" target="_blank">Flipkart</a>|<a href="http://amzn.to/sqlbook4onkindle" target="_blank">Kindle</a></strong>].</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/sql-puzzle/'>SQL Puzzle</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15845/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15845&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/leadlag1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/sqlinterview_s" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to LEAD and LAG &#8211; Analytic Functions Introduced in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/11/15/sql-server-introduction-to-lead-and-lag-analytic-functions-introduced-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/11/15/sql-server-introduction-to-lead-and-lag-analytic-functions-introduced-in-sql-server-2012/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 01:30:03 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15772</guid>
		<description><![CDATA[SQL Server 2012 introduces new analytical function LEAD() and LAG(). This functions accesses data from a subsequent row (for lead) and previous row (for lag) in the same result set without the use of a self-join . It will be very difficult to explain this in words so I will attempt small example to explain [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15772&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">SQL Server 2012 introduces new analytical function LEAD() and LAG(). This functions accesses data from a subsequent row (for lead) and previous row (for lag) in the same result set without the use of a self-join . It will be very difficult to explain this in words so I will attempt small example to explain you this function. Instead of creating new table, I will be using AdventureWorks sample database as most of the developer uses that for experiment.</p>
<p style="text-align:justify;">Let us fun following query.<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">LEAD</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAG</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">Above query will give us following result.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/leadlag1.jpg" alt="" width="500" /></p>
<p style="text-align:justify;">When we look at above resultset it is very clear that LEAD function gives us value which is going to come in next line and LAG function gives us value which was encountered in previous line. If we have to generate the same result without using this function we will have to use self join. In future blog post we will see the same. Let us explore this function a bit more. This function not only provide previous or next line but it can also access any line before or after using offset.</p>
<p style="text-align:justify;">Let us fun following query, where LEAD and LAG function accesses the row with offset of 2.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">LEAD</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAG</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Above query will give us following result.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/leadlag2.jpg" alt="" width="500" /></p>
<p style="text-align:justify;">You can see the LEAD and LAG functions  now have interval of  rows when they are returning results. As there is interval of two rows the first two rows in LEAD function and last two rows in LAG function will return NULL value. You can easily replace this NULL Value with any other default value by passing third parameter in LEAD and LAG function.</p>
<p style="text-align:justify;">Let us fun following query.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">LEAD</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAG</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Above query will give us following result, where NULL are now replaced with value 0.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/leadlag3.jpg" alt="" width="500" /></p>
<p style="text-align:justify;">Just like any other analytic function we can easily partition this function as well. Let us see the use of PARTITION BY in this clause.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">LEAD</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAG</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Above query will give us following result, where now the data is partitioned by SalesOrderID and LEAD and LAG functions are returning the appropriate result in that window. As now there are smaller partition in my query, you will see higher presence of NULL.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/leadlag4.jpg" alt="" width="500" /></p>
<p style="text-align:justify;">In future blog post we will see how this functions are compared to SELF JOIN.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15772/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15772&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/15/sql-server-introduction-to-lead-and-lag-analytic-functions-introduced-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/leadlag1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/leadlag2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/leadlag3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/leadlag4.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Puzzle to Win Print Book &#8211; Functions FIRST_VALUE and LAST_VALUE with OVER clause and ORDER BY</title>
		<link>http://blog.sqlauthority.com/2011/11/11/sql-server-puzzle-to-win-print-book-functions-first_value-and-last_value-with-over-clause-and-order-by/</link>
		<comments>http://blog.sqlauthority.com/2011/11/11/sql-server-puzzle-to-win-print-book-functions-first_value-and-last_value-with-over-clause-and-order-by/#comments</comments>
		<pubDate>Fri, 11 Nov 2011 01:30:02 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15697</guid>
		<description><![CDATA[Some time an interesting feature and smart audience makes total difference at places. From last two days, I have been writing on SQL Server 2012 feature FIRST_VALUE and LAST_VALUE. Please read following post before I continue today as this question is based on the same. Introduction to FIRST_VALUE and LAST_VALUE Introduction to FIRST_VALUE and LAST_VALUE [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15697&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Some time an interesting feature and smart audience makes total difference at places. From last two days, I have been writing on SQL Server 2012 feature FIRST_VALUE and LAST_VALUE.</p>
<p style="text-align:justify;">Please read following post before I continue today as this question is based on the same.</p>
<ul style="text-align:justify;">
<li><strong><a href="http://blog.sqlauthority.com/2011/11/09/sql-server-introduction-to-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012/" target="_blank">Introduction to FIRST_VALUE and LAST_VALUE</a></strong></li>
<li><strong><a href="http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/" target="_blank">Introduction to FIRST_VALUE and LAST_VALUE with OVER clause</a></strong></li>
</ul>
<p style="text-align:justify;">As a comment of the second post I received excellent question from <a href="http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/#comment-192018" target="_blank">Nilesh Molankar</a>. He asks what will happen if we change few things in the T-SQL. I really like this question as this kind of questions will make us sharp and help us perform in critical situation in need. We recently publish <strong><a href="http://blog.sqlauthority.com/sql-server-books/sql-server-interview-questions-and-answers-for-all-database-developers-and-developers-administrators/" target="_blank">SQL Server Interview Questions</a></strong> book. I promise that in future version of this book, we will for sure include this question. Instead of repeating his question, I am going to ask something very similar to his question.</p>
<p style="text-align:justify;">Let us first run following query (read <strong><a href="http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/" target="_blank">yesterday&#8217;s blog post</a></strong> for more detail):</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Here is the resultset of the above query.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/lastvalue1.jpg" alt="" width="491" height="264" /></p>
<p style="text-align:justify;">Now let us change the ORDER BY clause of OVER clause in above query and see what is the new result.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">OrderQty<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">OrderQty<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Now let us see the result and ready for interesting question:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/lastvalue4.jpg" alt="" width="498" height="259" /></p>
<h2 style="text-align:justify;">Puzzle</h2>
<p style="text-align:justify;">You can see that row number 2, 3, 4, and 5 has same SalesOrderID = 43667. The FIRST_VALUE is 78 and LAST_VALUE is 77. Now if these function was working on maximum and minimum value they should have given answer as 77 and 80 respectively instead of 78 and 77. Also the value of FIRST_VALUE is greater than LAST_VALUE 77. <strong>Why? Explain in detail.</strong></p>
<h2 style="text-align:justify;">Hint</h2>
<p style="text-align:justify;">Let me give you a simple hint. Just for simplicity I have changed the order of columns selected in the SELECT and ORDER BY (at the end). This will not change resultset but just order of the columns as well order of the rows. However, the data remains the same.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">OrderQty<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">OrderQty<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">Above query returns following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/lastvalue3.jpg" alt="" width="502" height="251" /></p>
<p style="text-align:justify;">Now I am very sure all of you have figured out the solution. Here is the<strong> second hint</strong> &#8211; pay attention to row 2, 3, 4, and 10.</p>
<h2 style="text-align:justify;">Hint2</h2>
<ul style="text-align:justify;">
<li><a href="http://beyondrelational.com/blogs/martinschmidt/archive/2011/08/30/t-sql-enhancements-first-value-and-last-value.aspx" target="_blank">T-SQL Enhancements: FIRST_VALUE() and LAST_VALUE()</a></li>
<li>MSDN: <a href="http://bit.ly/sDyZc4" target="_blank">FIRST_VALUE</a> and <a href="http://bit.ly/tjqtrP" target="_blank">LAST_VALUE</a></li>
</ul>
<h2 style="text-align:justify;">Rules</h2>
<ul style="text-align:justify;">
<li>Leave a comment with your detailed answer by Nov 15&#8242;s blog post.</li>
<li>Open world-wide (where Amazon ships books)</li>
<li>If you blog about puzzle&#8217;s solution and if you win, you win additional surprise gift as well.</li>
</ul>
<h2 style="text-align:justify;">Prizes</h2>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/sql-server-books/sql-server-interview-questions-and-answers-for-all-database-developers-and-developers-administrators/" target="_blank"><img class="alignnone" src="http://www.pinaldave.com/bimg/sqlinterview_s" alt="" width="169" height="258" /></a></p>
<p style="text-align:justify;">Print copy of my new book SQL Server Interview Questions <strong><a href="http://amzn.to/qTOXoG" target="_blank">Amazon</a>|<a href="http://bit.ly/indiasqlinterview" target="_blank">Flipkart</a></strong></p>
<p style="text-align:justify;">If you already have this book, you can opt for any of my other books SQL Wait Stats [<strong><a href="http://amzn.to/p3tL1n" target="_blank">Amazon</a>|<a href="http://amzn.to/waitstatskindle" target="_blank">Flipkart</a>|<a href="http://amzn.to/waitstatskindle" target="_blank">Kindle</a></strong>] and SQL Programming [<strong><a href="http://amzn.to/rsC2La" target="_blank">Amazon</a>|<a href="http://bit.ly/pinalbook4" target="_blank">Flipkart</a>|<a href="http://amzn.to/sqlbook4onkindle" target="_blank">Kindle</a></strong>].</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15697/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15697&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/11/sql-server-puzzle-to-win-print-book-functions-first_value-and-last_value-with-over-clause-and-order-by/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/lastvalue1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/lastvalue4.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/lastvalue3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/sqlinterview_s" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; OVER clause with FIRST _VALUE and LAST_VALUE  &#8211; Analytic Functions Introduced in SQL Server 2012 &#8211; ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING</title>
		<link>http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/</link>
		<comments>http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 01:30:28 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15683</guid>
		<description><![CDATA[Yesterday I had discussed two analytical functions FIRST_VALUE and LAST_VALUE. After reading the blog post I received very interesting question. &#8220;Don&#8217;t you think there is bug in your first example where FIRST_VALUE is remain same but the LAST_VALUE is changing every line. I think the LAST_VALUE should be the highest value in the windows or [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15683&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Yesterday I had discussed two analytical functions FIRST_VALUE and LAST_VALUE. After reading the blog post I received very interesting question.</p>
<p style="text-align:justify;">&#8220;Don&#8217;t you think there is bug in your first example where FIRST_VALUE is remain same but the LAST_VALUE is changing every line. I think the LAST_VALUE should be the highest value in the windows or set of result.&#8221;</p>
<p style="text-align:justify;">I find this question very interesting because this is very commonly made mistake. No there is no bug in the code. I think what we need is a bit more explanation. Let me attempt that first. Before you do that I suggest you <strong><a href="http://blog.sqlauthority.com/2011/11/09/sql-server-introduction-to-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012/" target="_blank">read yesterday&#8217;s blog post</a></strong> as this question is related to that blog post.</p>
<p style="text-align:justify;">Now let’s have fun following query:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/firstvalue1.jpg" alt="" width="500" height="278" /></p>
<p style="text-align:justify;">As per the reader&#8217;s question the value of the LAST_VALUE function should be always 114 and not increasing as the rows are increased. Let me re-write the above code once again with bit extra T-SQL Syntax. Please pay special attention to the ROW clause which I have added in the above syntax.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:blue;">CURRENT </span><span style="color:black;">ROW</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:blue;">CURRENT </span><span style="color:black;">ROW</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">Now once again check the result of the above query.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/firstvalue1.jpg" alt="" width="500" height="278" /></p>
<p style="text-align:justify;">The result of both the query is same because in OVER clause the default ROWS selection is always UNBOUNDED PRECEDING AND CURRENT ROW.</p>
<p style="text-align:justify;">If you want the maximum value of the windows with OVER clause you need to change the syntax to UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING for ROW clause. Now run following query and pay special attention to ROW clause again.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Here is the resultset of the above query which is what questioner was asking. So in simple word, there is no bug but there is additional syntax needed to add to get your desired answer.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/lastvalue1.jpg" alt="" width="491" height="264" /></p>
<p style="text-align:justify;">The same logic also applies to PARTITION BY clause when used. Here is quick example of how we can further partition the query by SalesOrderDetailID with this new functions.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Above query will give us windowed resultset on SalesOrderDetailsID as well give us FIRST and LAST value for the windowed resultset.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/lastvalue2.jpg" alt="" width="500" height="271" /></p>
<p style="text-align:justify;">There are lots to discuss for this two functions and we have just explored tip of the iceberg. In future post I will discover it further deep.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15683/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15683&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/firstvalue1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/firstvalue1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/lastvalue1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/lastvalue2.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to FIRST _VALUE and LAST_VALUE  &#8211; Analytic Functions Introduced in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/11/09/sql-server-introduction-to-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/11/09/sql-server-introduction-to-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012/#comments</comments>
		<pubDate>Wed, 09 Nov 2011 01:30:37 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15669</guid>
		<description><![CDATA[SQL Server 2012 introduces new analytical functions FIRST_VALUE() and LAST_VALUE(). This function returns first and last value from the list. It will be very difficult to explain this in words so I’d like to attempt to explain its function through a brief example. Instead of creating a new table, I will be using the AdventureWorks [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15669&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">SQL Server 2012 introduces new analytical functions FIRST_VALUE() and LAST_VALUE(). This function returns first and last value from the list. It will be very difficult to explain this in words so I’d like to attempt to explain its function through a brief example. Instead of creating a new table, I will be using the AdventureWorks sample database as most developers use that for experiment purposes.</p>
<p style="text-align:justify;">Now let’s have fun following query:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/firstvalue1.jpg" alt="" width="500" height="278" /></p>
<p style="text-align:justify;">What’s the most interesting thing here is that as we go from row 1 to row 10, the value of the FIRST_VALUE() remains the same but the value of the LAST_VALUE is increasing. The reason behind this is that as we progress in every line &#8211; considering that line and all the other lines before it, the last value will be of the row where we are currently looking at. To fully understand this statement, see the following figure:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/firstvalue2.jpg" alt="" width="500" height="287" /></p>
<p style="text-align:justify;">This may be useful in some cases; but not always. However, when we use the same thing with PARTITION BY, the same query starts showing the result which can be easily used in analytical algorithms and needs.</p>
<p style="text-align:justify;">Let us have fun through the following query:</p>
<p style="text-align:justify;">Let us fun following query.<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/firstvalue3.jpg" alt="" width="496" height="249" /></p>
<p style="text-align:justify;">Let us understand how PARTITION BY windows the resultset.</p>
<p style="text-align:justify;">I have used PARTITION BY SalesOrderID in my query. This will create small windows of the resultset from the original resultset and will follow the logic or FIRST_VALUE and LAST_VALUE in this resultset.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/firstvalue4.jpg" alt="" width="496" height="249" /></p>
<p style="text-align:justify;">Well, this is just an introduction to these functions. In the future blog posts we will go deeper to discuss the usage of these two functions. By the way, these functions can be applied over VARCHAR fields as well and are not limited to the numeric field only.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15669/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15669/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15669/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15669/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15669/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15669/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15669/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15669&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/09/sql-server-introduction-to-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/firstvalue1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/firstvalue2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/firstvalue3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/firstvalue4.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to CUME_DIST &#8211; Analytic Functions Introduced in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/11/08/sql-server-introduction-to-cume_dist-analytic-functions-introduced-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/11/08/sql-server-introduction-to-cume_dist-analytic-functions-introduced-in-sql-server-2012/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 01:30:07 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15651</guid>
		<description><![CDATA[This blog post is written in response to the T-SQL Tuesday post of Prox ‘n’ Funx. This is a very interesting subject. By the way Brad Schulz is my favorite guy when it is about blogging. I respect him as well learn a lot from him. Everybody is writing something new his subject, I decided [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15651&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><a href="http://bradsruminations.blogspot.com/2011/10/invitation-for-t-sql-tuesday-024-prox-n.html" target="_blank"><img class="alignleft" src="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" alt="" width="123" height="123" /></a>This blog post is written in response to the T-SQL Tuesday post of <a href="http://bradsruminations.blogspot.com/2011/10/invitation-for-t-sql-tuesday-024-prox-n.html" target="_blank">Prox ‘n’ Funx</a>. This is a very interesting subject. By the way Brad Schulz is my favorite guy when it is about blogging. I respect him as well learn a lot from him. Everybody is writing something new his subject, I decided to start SQL Server 2012 analytic functions series.</p>
<p style="text-align:justify;">SQL Server 2012 introduces new analytical function CUME_DIST(). This function provides cumulative distribution value. It will be very difficult to explain this in words so I will attempt small example to explain you this function. Instead of creating new table, I will be using AdventureWorks sample database as most of the developer uses that for experiment.</p>
<p style="text-align:justify;">Let us fun following query.<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">CUME_DIST</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">CDist<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">CDist </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">Above query will give us following result.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cumedist1.jpg" alt="" width="280" height="258" /></p>
<p style="text-align:justify;">Now let us understand what is the formula behind CUME_DIST and why the values in SalesOrderID = 43670 are 1.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cumedist3.jpg" alt="" width="501" height="289" /></p>
<p style="text-align:justify;">Let us take more example and be clear about why the values in SalesOrderID = 43667 are 0.5.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cumedist4.jpg" alt="" width="501" height="289" /></p>
<p style="text-align:justify;">Now let us enhence the same example and use PARTITION BY into the OVER clause and see the results. Run following query in SQL Server 2012.</p>
<p><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">,</span><span style="color:black;"> ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">CUME_DIST</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">CDist<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID </span><span style="color:blue;">DESC</span><span style="color:gray;">, </span><span style="color:black;">CDist </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">Now let us see the result of this query. We are have changed the ORDER BY clause as well partitioning by SalesOrderID.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cumedist5.jpg" alt="" width="363" height="248" /></p>
<p style="text-align:justify;">You can see that CUME_DIST() function provides us different results. Additionally now we see value 1 multiple times. As we are using partitioning for each group of SalesOrderID we get the CUME_DIST() value.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cumedist6.jpg" alt="" width="499" height="248" /></p>
<p style="text-align:justify;">CUME_DIST() was long awaited Analytical function and I am glad to see it in SQL Server 2012.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15651/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15651/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15651/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15651/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15651/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15651/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15651/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15651&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/08/sql-server-introduction-to-cume_dist-analytic-functions-introduced-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cumedist1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cumedist3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cumedist4.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cumedist5.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cumedist6.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Denali &#8211; 14 New Functions &#8211; A Quick Guide</title>
		<link>http://blog.sqlauthority.com/2011/09/21/sql-server-denali-14-new-functions-a-quick-guide/</link>
		<comments>http://blog.sqlauthority.com/2011/09/21/sql-server-denali-14-new-functions-a-quick-guide/#comments</comments>
		<pubDate>Wed, 21 Sep 2011 01:30:37 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=14519</guid>
		<description><![CDATA[Last two weeks I have written various blog post on new functions introduced in SQL Server Denali. So many comments and request I have received from various readers that they would like to see everything together. I have put up a quick guide here where I am writing all the 14 new functions linking them [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14519&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Last two weeks I have written various blog post on new functions introduced in SQL Server Denali. So many comments and request I have received from various readers that they would like to see everything together. I have put up a quick guide here where I am writing all the 14 new functions linking them to my blog post as well Book On-Line for a quick reference.</p>
<table border="1" cellspacing="0" cellpadding="7">
<tbody>
<tr>
<td><strong>SQLAuthority.com</strong></td>
<td><strong>Book On-Line</strong></td>
</tr>
<tr>
<td style="text-align:center;" colspan="2"><strong>Conversion functions</strong></td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/06/sql-server-denali-conversion-function-parse-a-quick-introduction/" target="_blank">PARSE</a></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213316%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">PARSE</a></td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/07/sql-server-denali-conversion-function-try_parse-a-quick-introduction/" target="_blank">TRY_CONVERT</a></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh230993%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">TRY_CONVERT</a></td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/08/sql-server-denali-conversion-function-try_convert-a-quick-introduction/" target="_blank">TRY_PARSE</a></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213126%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">TRY_PARSE</a></td>
</tr>
<tr>
<td style="text-align:center;" colspan="2"><strong>Date and time functions</strong></td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/19/sql-server-denali-date-and-time-functions-datefromparts-datetimefromparts-datetime2fromparts-timefromparts-smalldatetimefromparts-datetimeoffsetfromparts-a-quick-introduc/" target="_blank">DATEFROMPARTS</a></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213228%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">DATEFROMPARTS</a></td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/19/sql-server-denali-date-and-time-functions-datefromparts-datetimefromparts-datetime2fromparts-timefromparts-smalldatetimefromparts-datetimeoffsetfromparts-a-quick-introduc/" target="_blank">DATETIME2FROMPARTS</a></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213312%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">DATETIME2FROMPARTS</a></td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/19/sql-server-denali-date-and-time-functions-datefromparts-datetimefromparts-datetime2fromparts-timefromparts-smalldatetimefromparts-datetimeoffsetfromparts-a-quick-introduc/" target="_blank">DATETIMEFROMPARTS</a></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213233%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">DATETIMEFROMPARTS</a></td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/19/sql-server-denali-date-and-time-functions-datefromparts-datetimefromparts-datetime2fromparts-timefromparts-smalldatetimefromparts-datetimeoffsetfromparts-a-quick-introduc/" target="_blank">DATETIMEOFFSETFROMPARTS</a>      </td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh231077%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">DATETIMEOFFSETFROMPARTS</a>      </td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/20/sql-server-denali-date-and-time-functions-eomonth-a-quick-introduction/" target="_blank">EOMONTH</a></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213020%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">EOMONTH</a></td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/19/sql-server-denali-date-and-time-functions-datefromparts-datetimefromparts-datetime2fromparts-timefromparts-smalldatetimefromparts-datetimeoffsetfromparts-a-quick-introduc/" target="_blank">SMALLDATETIMEFROMPARTS</a></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213396%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">SMALLDATETIMEFROMPARTS</a></td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/19/sql-server-denali-date-and-time-functions-datefromparts-datetimefromparts-datetime2fromparts-timefromparts-smalldatetimefromparts-datetimeoffsetfromparts-a-quick-introduc/" target="_blank">TIMEFROMPARTS</a></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213398%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">TIMEFROMPARTS</a></td>
</tr>
<tr>
<td style="text-align:center;" colspan="2"><strong>Logical functions</strong></td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/11/sql-server-denali-logical-function-choose-a-quick-introduction/" target="_blank">CHOOSE</a></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213019%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">CHOOSE</a></td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/10/sql-server-denali-logical-function-iif-a-quick-introduction/" target="_blank">IIF</a></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213574%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">IIF</a></td>
</tr>
<tr>
<td style="text-align:center;" colspan="2"><strong>String functions</strong></td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/16/sql-server-denali-string-function-concat-a-quick-introduction/" target="_blank">CONCAT</a></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh231515%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">CONCAT</a></td>
</tr>
<tr>
<td><a href="http://blog.sqlauthority.com/2011/09/17/sql-server-denali-string-function-format-a-quick-introduction/" target="_blank">FORMAT</a></td>
<td><a href="http://msdn.microsoft.com/en-us/library/hh213505%28v=SQL.110%29.aspx?ocid=aff-n-in-loc--pd" target="_blank">FORMAT</a></td>
</tr>
</tbody>
</table>
<p style="text-align:justify;">I have personally bookmarked this post for my future reference.</p>
<p style="text-align:justify;">Reference:<strong></strong> <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/14519/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/14519/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/14519/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/14519/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/14519/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/14519/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/14519/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/14519/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/14519/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/14519/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/14519/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/14519/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/14519/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/14519/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14519&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/09/21/sql-server-denali-14-new-functions-a-quick-guide/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Denali &#8211; Date and Time Functions &#8211; EOMONTH() &#8211; A Quick Introduction</title>
		<link>http://blog.sqlauthority.com/2011/09/20/sql-server-denali-date-and-time-functions-eomonth-a-quick-introduction/</link>
		<comments>http://blog.sqlauthority.com/2011/09/20/sql-server-denali-date-and-time-functions-eomonth-a-quick-introduction/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 01:30:19 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=14475</guid>
		<description><![CDATA[In SQL Server Denali, seven new datetime functions have been introduced, namely, DATEFROMPARTS (year, month, day) DATETIME2FROMPARTS (year, month, day, hour, minute, seconds, fractions, precision) DATETIMEFROMPARTS (year, month, day, hour, minute, seconds, milliseconds) DATETIMEOFFSETFROMPARTS (year, month, day, hour, minute, seconds, fractions, hour_offset, minute_offset, precision) SMALLDATETIMEFROMPARTS (year, month, day, hour, minute) TIMEFROMPARTS (hour, minute, seconds, fractions, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14475&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">In SQL Server Denali, seven new datetime functions have been introduced, namely,</p>
<ul style="text-align:justify;">
<li>DATEFROMPARTS (year, month, day)</li>
<li>DATETIME2FROMPARTS (year, month, day, hour, minute, seconds, fractions, precision)</li>
<li>DATETIMEFROMPARTS (year, month, day, hour, minute, seconds, milliseconds)</li>
<li>DATETIMEOFFSETFROMPARTS (year, month, day, hour, minute, seconds, fractions, hour_offset, minute_offset, precision)</li>
<li>SMALLDATETIMEFROMPARTS (year, month, day, hour, minute)</li>
<li>TIMEFROMPARTS (hour, minute, seconds, fractions, precision)</li>
<li><strong>EOMONTH (start_date)</strong></li>
</ul>
<p style="text-align:justify;">EOMONTH() is a very interesting function. It is a very common requirement in many major applications where the user needs the last day of the month. It is very easy to figure out what is the first day of the month because obviously, it is comes first anyway. However, the last day of the month keeps changing as every month has different number of days and leap year also makes this more interesting.</p>
<p style="text-align:justify;">Let us understand this function using few examples.</p>
<p style="text-align:justify;"><strong>Example 1: Use of EOMONTH() function to find last day of the month</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:teal;">EOMONTH</span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()) </span><span style="color:teal;">LastDayofMonth<br />
</span></code><br />
<img class="alignnone" src="http://www.pinaldave.com/bimg/eomonth1.jpg" alt="" width="500" /><br />
<strong>Example 2: Use of EOMONTH() function to find last day of the month during Leapyear</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">EOMONTH</span><span style="color:gray;">(</span><span style="color:red;">'20110201'</span><span style="color:gray;">) </span><span style="color:black;">NonLeapYearFebLastDay</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">EOMONTH</span><span style="color:gray;">(</span><span style="color:red;">'20120201'</span><span style="color:gray;">) </span><span style="color:black;">LeapYearFebLastDay</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">EOMONTH</span><span style="color:gray;">(</span><span style="color:red;">'20130201'</span><span style="color:gray;">) </span><span style="color:black;">NonLeapYearFebLastDay</span><span style="color:gray;">;</span></code><br />
<img class="alignnone" src="http://www.pinaldave.com/bimg/eomonth2.jpg" alt="" width="500" /><br />
<strong>Example 3: Use of EOMONTH() function to find last day Previous and Next Month</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:teal;">EOMONTH</span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(),-</span><span style="color:teal;">1</span><span style="color:gray;">) </span><span style="color:teal;">PreviousMonthLastDay</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">EOMONTH</span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(),</span><span style="color:teal;">1</span><span style="color:gray;">) </span><span style="color:teal;">NextMonthLastDay</span><span style="color:gray;">;<br />
</span></code><br />
<img class="alignnone" src="http://www.pinaldave.com/bimg/eomonth3.jpg" alt="" width="500" /><br />
<strong>Example 4: Use of EOMONTH() function to find last day Previous and Next Month</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:teal;">dw</span><span style="color:gray;">,</span><span style="color:teal;">EOMONTH</span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">())) </span><span style="color:teal;">LastDayofMonthDay</span><span style="color:gray;">;</span></code><br />
<img class="alignnone" src="http://www.pinaldave.com/bimg/eomonth4.jpg" alt="" width="500" /><br />
<strong>Example 5: Get First Date of Next Week</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:teal;">d</span><span style="color:gray;">,</span><span style="color:teal;">1</span><span style="color:gray;">,</span><span style="color:teal;">EOMONTH</span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">())) </span><span style="color:teal;">NextMonthFirstDay</span><span style="color:gray;">;</span></code><br />
<img class="alignnone" src="http://www.pinaldave.com/bimg/eomonth5.jpg" alt="" width="500" /><br />
Have you tried the following method? I personally use the method mentioned above but once in an interview, one of the candidates gave the following answer, and I found it very interesting.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(</span><span style="color:teal;">EOMONTH</span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(),</span><span style="color:teal;">1</span><span style="color:gray;">),</span><span style="color:teal;">31</span><span style="color:gray;">,</span><span style="color:teal;">1</span><span style="color:gray;">) </span><span style="color:teal;">NextMonthFirstDay</span><span style="color:gray;">;</span></code><br />
<img class="alignnone" src="http://www.pinaldave.com/bimg/eomonth6.jpg" alt="" width="500" /><br />
Well, you can also apply this function to any table column to get the desired value. I have previously written an UDF when I had the following requirement: when any user registers in an application, he should get automatically validated till the end of the month. I will now replace that custom logic/UDF with this function.</p>
<p style="text-align:justify;">Reference:<strong></strong> <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/14475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/14475/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/14475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/14475/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/14475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/14475/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/14475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/14475/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/14475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/14475/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/14475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/14475/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/14475/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/14475/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14475&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/09/20/sql-server-denali-date-and-time-functions-eomonth-a-quick-introduction/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/eomonth1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/eomonth2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/eomonth3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/eomonth4.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/eomonth5.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/eomonth6.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Denali &#8211; Date and Time Functions &#8211; DATEFROMPARTS() &#8211; DATETIMEFROMPARTS() &#8211; DATETIME2FROMPARTS() &#8211; TIMEFROMPARTS() &#8211; SMALLDATETIMEFROMPARTS() &#8211; DATETIMEOFFSETFROMPARTS() &#8211; A Quick Introduction</title>
		<link>http://blog.sqlauthority.com/2011/09/19/sql-server-denali-date-and-time-functions-datefromparts-datetimefromparts-datetime2fromparts-timefromparts-smalldatetimefromparts-datetimeoffsetfromparts-a-quick-introduc/</link>
		<comments>http://blog.sqlauthority.com/2011/09/19/sql-server-denali-date-and-time-functions-datefromparts-datetimefromparts-datetime2fromparts-timefromparts-smalldatetimefromparts-datetimeoffsetfromparts-a-quick-introduc/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 01:30:04 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=14458</guid>
		<description><![CDATA[In SQL Server Denali, there are seven new datetime functions being introduced, namely: DATEFROMPARTS ( year, month, day) DATETIME2FROMPARTS ( year, month, day, hour, minute, seconds, fractions, precision ) DATETIMEFROMPARTS ( year, month, day, hour, minute, seconds, milliseconds ) DATETIMEOFFSETFROMPARTS ( year, month, day, hour, minute, seconds, fractions, hour_offset, minute_offset, precision ) SMALLDATETIMEFROMPARTS ( year, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14458&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">In SQL Server Denali, there are seven new datetime functions being introduced, namely:</p>
<ul style="text-align:justify;">
<li><strong>DATEFROMPARTS ( year, month, day) </strong></li>
<li><strong>DATETIME2FROMPARTS ( year, month, day, hour, minute, seconds, fractions, precision )</strong></li>
<li><strong>DATETIMEFROMPARTS ( year, month, day, hour, minute, seconds, milliseconds )</strong></li>
<li><strong>DATETIMEOFFSETFROMPARTS ( year, month, day, hour, minute, seconds, fractions, hour_offset, minute_offset, precision )</strong></li>
<li><strong>SMALLDATETIMEFROMPARTS ( year, month, day, hour, minute )</strong></li>
<li><strong>TIMEFROMPARTS ( hour, minute, seconds, fractions, precision )</strong></li>
<li>EOMONTH ()</li>
</ul>
<p style="text-align:justify;">Today we will quickly look at six of the above functions. I really wanted to write a separate blog post for all of them but once you see example here you will understand why they are very simple. First six functions have one thing in common among them. They all build datetime value from parameters passed into the function. Let us see the quick example.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:teal;">DATEFROMPARTS </span><span style="color:gray;">(</span><span style="color:teal;">2010</span><span style="color:gray;">,</span><span style="color:teal;">12</span><span style="color:gray;">,</span><span style="color:teal;">31</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">DATETIME2FROMPARTS </span><span style="color:gray;">(</span><span style="color:teal;">2010</span><span style="color:gray;">,</span><span style="color:teal;">12</span><span style="color:gray;">,</span><span style="color:teal;">31</span><span style="color:gray;">,</span><span style="color:teal;">23</span><span style="color:gray;">,</span><span style="color:teal;">59</span><span style="color:gray;">,</span><span style="color:teal;">59</span><span style="color:gray;">,</span><span style="color:teal;">0</span><span style="color:gray;">,</span><span style="color:teal;">0</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">DATETIMEFROMPARTS </span><span style="color:gray;">(</span><span style="color:teal;">2010</span><span style="color:gray;">,</span><span style="color:teal;">12</span><span style="color:gray;">,</span><span style="color:teal;">31</span><span style="color:gray;">,</span><span style="color:teal;">23</span><span style="color:gray;">,</span><span style="color:teal;">59</span><span style="color:gray;">,</span><span style="color:teal;">59</span><span style="color:gray;">,</span><span style="color:teal;">0</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">DATETIMEOFFSETFROMPARTS </span><span style="color:gray;">(</span><span style="color:teal;">2010</span><span style="color:gray;">,</span><span style="color:teal;">12</span><span style="color:gray;">,</span><span style="color:teal;">31</span><span style="color:gray;">,</span><span style="color:teal;">14</span><span style="color:gray;">,</span><span style="color:teal;">23</span><span style="color:gray;">,</span><span style="color:teal;">23</span><span style="color:gray;">,</span><span style="color:teal;">0</span><span style="color:gray;">,</span><span style="color:teal;">12</span><span style="color:gray;">,</span><span style="color:teal;">0</span><span style="color:gray;">,</span><span style="color:teal;">7</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">SMALLDATETIMEFROMPARTS </span><span style="color:gray;">(</span><span style="color:teal;">2010</span><span style="color:gray;">,</span><span style="color:teal;">12</span><span style="color:gray;">,</span><span style="color:teal;">31</span><span style="color:gray;">,</span><span style="color:teal;">23</span><span style="color:gray;">,</span><span style="color:teal;">59</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Result<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">TIMEFROMPARTS </span><span style="color:gray;">(</span><span style="color:teal;">23</span><span style="color:gray;">,</span><span style="color:teal;">59</span><span style="color:gray;">,</span><span style="color:teal;">59</span><span style="color:gray;">,</span><span style="color:teal;">0</span><span style="color:gray;">,</span><span style="color:teal;">0</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Result</span><span style="color:gray;">;<br />
</span></code></p>
<p style="text-align:justify;">Now let us see the result of the above script.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/date2frompart.jpg" alt="" width="587" height="588" /></p>
<p class="MsoNormal" style="text-align:justify;line-height:normal;"><span style="font-size:12pt;font-family:'Times New Roman',serif;">One of the interesting points to note is that in the </span><span style="font-size:10pt;font-family:'Courier New';">DATETIME2FROMPARTS, </span><span style="font-size:12pt;font-family:'Times New Roman',serif;">the last parameter is the precision of datetime; it represents the fraction of the second. Here I would like to get back to you with a simple question, </span></p>
<p class="MsoNormal" style="text-align:justify;line-height:normal;"><strong><span style="font-size:12pt;font-family:'Times New Roman',serif;">“What is your opinion on these new functions and where exactly will you use them in real life?”</span></strong></p>
<p class="MsoNormal" style="text-align:justify;line-height:normal;"><span style="font-size:12pt;font-family:'Times New Roman',serif;">I am looking for creative answers, but each should be close to real-world usage.</span></p>
<p style="text-align:justify;">Reference:<strong></strong> <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/14458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/14458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/14458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/14458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/14458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/14458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/14458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/14458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/14458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/14458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/14458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/14458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/14458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/14458/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14458&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/09/19/sql-server-denali-date-and-time-functions-datefromparts-datetimefromparts-datetime2fromparts-timefromparts-smalldatetimefromparts-datetimeoffsetfromparts-a-quick-introduc/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/date2frompart.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Denali &#8211; String Function &#8211; FORMAT() &#8211; A Quick Introduction</title>
		<link>http://blog.sqlauthority.com/2011/09/17/sql-server-denali-string-function-format-a-quick-introduction/</link>
		<comments>http://blog.sqlauthority.com/2011/09/17/sql-server-denali-string-function-format-a-quick-introduction/#comments</comments>
		<pubDate>Sat, 17 Sep 2011 01:30:15 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=14424</guid>
		<description><![CDATA[In SQL Server Denali, there are two new string functions being introduced, namely: CONCAT() FORMAT() Today we will quickly take a look at the FORMAT() function. FORMAT converts the first argument to specified format and returns the string value. This function is locale-aware and it can return the formatting of the datetime and number to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14424&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">In SQL Server Denali, there are two new string functions being introduced, namely:</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2011/09/16/sql-server-denali-string-function-concat-a-quick-introduction/" target="_blank">CONCAT()</a><strong><br />
<a href="http://blog.sqlauthority.com/2011/09/17/sql-server-denali-string-function-format-a-quick-introduction/" target="_blank">FORMAT()</a><br />
</strong></p>
<p style="text-align:justify;">Today we will quickly take a look at the FORMAT() function. FORMAT converts the first argument to specified format and returns the string value. This function is locale-aware and it can return the formatting of the datetime and number to as per the locale specified string. This function also uses the server .NET Framework and CLR.</p>
<p style="text-align:justify;">I was personally waiting for this function for long time and inclusion of this function made me very happy as this single function will solve lots of formatting issues for developer and leading them not to write bad code (sometime).</p>
<p style="text-align:justify;">Now let us look at these examples showing how FORMAT() works:</p>
<p style="text-align:justify;"><strong>Example 1: Location Aware FORMAT function with Date<br />
</strong></p>
<p><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@d </span><span style="color:blue;">DATETIME = </span><span style="color:red;">'01/01/2011'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:#434343;">@d</span><span style="color:gray;">, </span><span style="color:red;">'d'</span><span style="color:gray;">, </span><span style="color:red;">'en-US' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">US_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:#434343;">@d</span><span style="color:gray;">, </span><span style="color:red;">'d'</span><span style="color:gray;">, </span><span style="color:red;">'fr-FR' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">FR_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:#434343;">@d</span><span style="color:gray;">, </span><span style="color:red;">'d'</span><span style="color:gray;">, </span><span style="color:red;">'de-DE' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">DE_Result</span><span style="color:gray;">;<br />
</span><span style="color:teal;">GO<br />
</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/format1.jpg" alt="" width="500" /></p>
<p style="text-align:justify;"><strong>Example 2: Location Aware FORMAT function with Currency<br />
</strong></p>
<p><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@d </span><span style="color:blue;">INT = </span><span style="color:teal;">500</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:#434343;">@d</span><span style="color:gray;">, </span><span style="color:red;">'c'</span><span style="color:gray;">, </span><span style="color:red;">'en-US' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">US_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:#434343;">@d</span><span style="color:gray;">, </span><span style="color:red;">'c'</span><span style="color:gray;">, </span><span style="color:red;">'fr-FR' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">FR_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:#434343;">@d</span><span style="color:gray;">, </span><span style="color:red;">'c'</span><span style="color:gray;">, </span><span style="color:red;">'de-DE' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">DE_Result</span><span style="color:gray;">;<br />
</span><span style="color:teal;">GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/format2.jpg" alt="" width="485" height="353" /></p>
<p style="text-align:justify;"><strong>Example 3: Various format of Datetime</strong></p>
<p><code style="font-size:12px;"><span style="color:green;">-- Day<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">'d'</span><span style="color:gray;">, </span><span style="color:red;">'en-US' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">US_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">'dd'</span><span style="color:gray;">, </span><span style="color:red;">'en-US' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">US_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">'ddd'</span><span style="color:gray;">, </span><span style="color:red;">'en-US' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">US_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">'dddd'</span><span style="color:gray;">, </span><span style="color:red;">'en-US' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">US_Result</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/format3.jpg" alt="" width="500" /></p>
<p><code style="font-size:12px;"><span style="color:green;">-- Minutes<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">'m'</span><span style="color:gray;">, </span><span style="color:red;">'en-US' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">US_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">'mm'</span><span style="color:gray;">, </span><span style="color:red;">'en-US' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">US_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">'mmm'</span><span style="color:gray;">, </span><span style="color:red;">'en-US' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">US_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">'mmmm'</span><span style="color:gray;">, </span><span style="color:red;">'en-US' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">US_Result</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/format4.jpg" alt="" width="500" /></p>
<p style="text-align:justify;">Please note, for months use &#8216;M&#8217; (capital) and not the small &#8216;m&#8217;. In my example I used &#8216;m&#8217; which displays the minutes. Thanks to Aaron Bertrand (<a href="http://sqlblog.com/blogs/aaron_bertrand/" target="_blank">blog</a>|<a href="http://twitter.com/AaronBertrand" target="_blank">twitter</a>) for pointing the error.</p>
<p><code style="font-size:12px;"><span style="color:green;">-- Year<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">'y'</span><span style="color:gray;">, </span><span style="color:red;">'en-US' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">US_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">'yy'</span><span style="color:gray;">, </span><span style="color:red;">'en-US' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">US_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">( </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">'yyy'</span><span style="color:gray;">, </span><span style="color:red;">'en-US' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">US_Result</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/format5.jpg" alt="" width="500" /></p>
<p style="text-align:justify;"><strong>Example 4: Various format of Currency<br />
</strong></p>
<p><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@var </span><span style="color:blue;">INT = </span><span style="color:teal;">50<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT</span><span style="color:gray;">(</span><span style="color:#434343;">@var</span><span style="color:gray;">,</span><span style="color:red;">'c'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Currency</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT</span><span style="color:gray;">(</span><span style="color:#434343;">@var</span><span style="color:gray;">,</span><span style="color:red;">'c1'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Currency</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT</span><span style="color:gray;">(</span><span style="color:#434343;">@var</span><span style="color:gray;">,</span><span style="color:red;">'c2'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Currency</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT</span><span style="color:gray;">(</span><span style="color:#434343;">@var</span><span style="color:gray;">,</span><span style="color:red;">'c3'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Currency</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/format7.jpg" alt="" width="500" /></p>
<p><strong>Example 5: Various miscalculation format<br />
</strong><br />
<code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@var </span><span style="color:blue;">INT = </span><span style="color:teal;">50<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT</span><span style="color:gray;">(</span><span style="color:#434343;">@var</span><span style="color:gray;">,</span><span style="color:red;">'p'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Percentage</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT</span><span style="color:gray;">(</span><span style="color:#434343;">@var</span><span style="color:gray;">,</span><span style="color:red;">'e'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Scientific</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT</span><span style="color:gray;">(</span><span style="color:#434343;">@var</span><span style="color:gray;">,</span><span style="color:red;">'x'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Hexa</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT</span><span style="color:gray;">(</span><span style="color:#434343;">@var</span><span style="color:gray;">,</span><span style="color:red;">'x4'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Hexa1</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/format8.jpg" alt="" width="500" /></p>
<p style="text-align:justify;"><strong>Example 6: Format returning results in various language</strong></p>
<p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">N'dddd MMMM dd, yyyy'</span><span style="color:gray;">, </span><span style="color:red;">'en-US'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">English_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">N'dddd MMMM dd, yyyy'</span><span style="color:gray;">, </span><span style="color:red;">'hi'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Hindi_Result</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">FORMAT </span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:red;">N'dddd MMMM dd, yyyy'</span><span style="color:gray;">, </span><span style="color:red;">'gu'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Gujarati_Result</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/format9.jpg" alt="" width="500" /></p>
<p style="text-align:justify;">This all the functions can easily work the table column as well. Here is the quick example of the same.</p>
<p style="text-align:justify;"><strong>Example 7: Format returning results based on table column</strong></p>
<p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:teal;">ModifiedDate</span><span style="color:gray;">,<br />
</span><span style="color:teal;">FORMAT</span><span style="color:gray;">(</span><span style="color:teal;">ModifiedDate</span><span style="color:gray;">, </span><span style="color:red;">N'dddd MMMM dd, yyyy'</span><span style="color:gray;">,</span><span style="color:red;">'fr'</span><span style="color:gray;">) </span><span style="color:teal;">Fr_ModifiedDate</span><span style="color:gray;">,<br />
</span><span style="color:teal;">UnitPrice</span><span style="color:gray;">,<br />
</span><span style="color:teal;">FORMAT</span><span style="color:gray;">(</span><span style="color:teal;">UnitPrice</span><span style="color:gray;">, </span><span style="color:red;">'c'</span><span style="color:gray;">,</span><span style="color:red;">'fr'</span><span style="color:gray;">) </span><span style="color:teal;">Fr_UnitPrice<br />
</span><span style="color:blue;">FROM </span><span style="color:teal;">Sales.SalesOrderDetail</span></code></p>
<p>You can see that dates and price are now converted to French locale.</p>
<p><img class="alignnone" src="http://www.pinaldave.com/bimg/format10.jpg" alt="" width="500" /></p>
<p>I think FORMAT is the most important function introduced in SQL Server Denali. It has made life easier for every developer.</p>
<p style="text-align:justify;">Reference:<strong></strong>  <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/14424/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/14424/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/14424/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/14424/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/14424/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/14424/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/14424/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/14424/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/14424/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/14424/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/14424/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/14424/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/14424/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/14424/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14424&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/09/17/sql-server-denali-string-function-format-a-quick-introduction/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/format1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/format2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/format3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/format4.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/format5.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/format7.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/format8.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/format9.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/format10.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Denali &#8211; String Function &#8211; CONCAT() &#8211; A Quick Introduction</title>
		<link>http://blog.sqlauthority.com/2011/09/16/sql-server-denali-string-function-concat-a-quick-introduction/</link>
		<comments>http://blog.sqlauthority.com/2011/09/16/sql-server-denali-string-function-concat-a-quick-introduction/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 01:30:31 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=14394</guid>
		<description><![CDATA[In SQL Server Denali, there are two new string functions being introduced, namely: CONCAT() FORMAT() Today we will quickly take a look at the CONCAT() function. CONCAT takes a minimum of two arguments to concatenate them, resulting to a single string. Now let us look at these examples showing how CONCAT() works: Example 1: CONCAT [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14394&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">In SQL Server Denali, there are two new string functions being introduced, namely:</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2011/09/16/sql-server-denali-string-function-concat-a-quick-introduction/" target="_blank"><strong>CONCAT()</strong></a><br />
<a href="http://blog.sqlauthority.com/2011/09/17/sql-server-denali-string-function-format-a-quick-introduction/" target="_blank">FORMAT()</a></p>
<p style="text-align:justify;">Today we will quickly take a look at the CONCAT() function. CONCAT takes a minimum of two arguments to concatenate them, resulting to a single string.</p>
<p style="text-align:justify;">Now let us look at these examples showing how CONCAT() works:</p>
<p style="text-align:justify;"><strong>Example 1: CONCAT Function Usage<br />
</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:teal;">CONCAT</span><span style="color:gray;">(</span><span style="color:teal;">1</span><span style="color:gray;">, </span><span style="color:teal;">2</span><span style="color:gray;">, </span><span style="color:teal;">3</span><span style="color:gray;">, </span><span style="color:teal;">4</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">SingleString<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">CONCAT</span><span style="color:gray;">(</span><span style="color:red;">'One'</span><span style="color:gray;">,</span><span style="color:teal;">1</span><span style="color:gray;">, </span><span style="color:teal;">1.1</span><span style="color:gray;">, </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()) </span><span style="color:blue;">AS </span><span style="color:teal;">SingleString<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">CONCAT</span><span style="color:gray;">(</span><span style="color:red;">'One'</span><span style="color:gray;">,</span><span style="color:teal;">2</span><span style="color:gray;">,NULL) </span><span style="color:blue;">AS </span><span style="color:teal;">SingleString<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">CONCAT</span><span style="color:gray;">(</span><span style="color:red;">''</span><span style="color:gray;">,</span><span style="color:red;">''</span><span style="color:gray;">,</span><span style="color:red;">''</span><span style="color:gray;">,</span><span style="color:red;">''</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">SingleString<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">CONCAT</span><span style="color:gray;">(NULL, NULL) </span><span style="color:blue;">AS </span><span style="color:teal;">SingleString</span></code></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:gray;"><img class="alignnone" src="http://www.pinaldave.com/bimg/concat1.jpg" alt="" width="501" height="432" /><br />
</span></code></p>
<p style="text-align:justify;">Now let us observe a few things based on the result above. Earlier when we had to concat strings, we used &#8216;+&#8217; sign and always CAST/CONVERT any variable to string. It used to give us an error. However, when you look at this new one function, it automatically and implicitly CAST/CONVERT any datatype to integer and then CONCATs them together in a single string. NULL values are automatically converted to empty strings. If you notice that even the datetime fields are automatically converted to the string without any extra operations. Additionally, the return value from the CONCAT string could be of datatype VARCHAR(MAX).</p>
<p style="text-align:justify;"><strong>Example 2: Usage of CONCAT with Table</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:teal;">[AdventureWorks2008R2]<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">CONCAT</span><span style="color:gray;">(</span><span style="color:teal;">[AddressID]</span><span style="color:gray;">,</span><span style="color:red;">' '</span><span style="color:gray;">,</span><span style="color:teal;">[AddressLine1]</span><span style="color:gray;">,</span><span style="color:red;">' '</span><span style="color:gray;">,<br />
</span><span style="color:teal;">[AddressLine2]</span><span style="color:gray;">,</span><span style="color:red;">' '</span><span style="color:gray;">,</span><span style="color:teal;">[City]</span><span style="color:gray;">,</span><span style="color:red;">' '</span><span style="color:gray;">,<br />
</span><span style="color:teal;">[StateProvinceID]</span><span style="color:gray;">,</span><span style="color:red;">' '</span><span style="color:gray;">,</span><span style="color:teal;">[PostalCode]</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Address<br />
</span><span style="color:blue;">FROM </span><span style="color:teal;">[Person].[Address]<br />
GO</span></code></p>
<p style="text-align:justify;">In the following example, we see the result of the query listed above: <code style="font-size:12px;"></code></p>
<p style="text-align:justify;"><a href="http://www.pinaldave.com/bimg/choose2.jpg"><img class="alignnone" src="http://www.pinaldave.com/bimg/concat2.jpg" alt="" width="500" /></a></p>
<p style="text-align:justify;">You can see how neatly and easily the strings are concatenated using this new function. It takes out lots of unnecessary code and makes it much simpler to execute.<br />
<img class="alignnone" src="http://www.pinaldave.com/bimg/concat4.jpg" alt="" width="502" height="132" /></p>
<p style="text-align:justify;">Now let us look under the hood in the execution plan. In the execution plan we can see that CONCAT function is a scalar operation. When we look at the scalar operation using Properties, it shows that the CONVERT_IMPLICIT function is automatically called to convert non-nvarchar datatypes columns to NVARCHAR.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/concat3.jpg" alt="" width="471" height="336" /></p>
<p style="text-align:justify;">Overall, when I have to concat multiple values and data in the future, I am going to use the CONCATE() function.</p>
<p style="text-align:justify;">Reference:<strong></strong>  <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/14394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/14394/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/14394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/14394/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/14394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/14394/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/14394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/14394/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/14394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/14394/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/14394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/14394/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/14394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/14394/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14394&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/09/16/sql-server-denali-string-function-concat-a-quick-introduction/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/concat1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/concat2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/concat4.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/concat3.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Denali &#8211; New Functions and Shorthand for CASE Statement</title>
		<link>http://blog.sqlauthority.com/2011/09/13/sql-server-denali-new-functions-and-shorthand-for-case-statement-2/</link>
		<comments>http://blog.sqlauthority.com/2011/09/13/sql-server-denali-new-functions-and-shorthand-for-case-statement-2/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 01:30:47 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=14734</guid>
		<description><![CDATA[This blog post is written in response to the T-SQL Tuesday post of Data Presentation. This is a very interesting subject. I recently started to write about Denali Logical and Comparison functions. I really enjoyed writing about new functions, but there was one question kept cropping up &#8211; is the CASE statement being replaced with [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14734&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><img class="alignleft" src="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" alt="" width="123" height="123" /></p>
<p style="text-align:justify;">This blog post is written in response to the T-SQL Tuesday post of <a href="http://www.sqlservercentral.com/blogs/pearlknows/archive/2011/09/06/invitation-for-t-sql-tuesday-22-data-presentation.aspx" target="_blank">Data Presentation</a><strong>.</strong> This is a very interesting subject. I recently started to write about Denali Logical and Comparison functions.</p>
<p style="text-align:justify;">I really enjoyed writing about new functions, but there was one question kept cropping up &#8211; is the CASE statement being replaced with this new functions. The answer is NO. New functions that are introduced are just shorthand for the CASE statement, and they are not replacing anything.</p>
<p style="text-align:justify;">1) <a href="http://blog.sqlauthority.com/2011/09/07/sql-server-denali-conversion-function-try_parse-a-quick-introduction/" target="_blank"><strong>TRY_PARSE()</strong></a> is not replacing the CASE statement, infect it is not. However, it can be smartly used along with the CASE statement. Here is the example in which it is used along with the CASE statement.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT<br />
</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:teal;">TRY_PARSE</span><span style="color:gray;">(</span><span style="color:red;">'A100.000' </span><span style="color:blue;">AS INT</span><span style="color:gray;">) </span><span style="color:blue;">IS </span><span style="color:gray;">NULL<br />
</span><span style="color:blue;">THEN </span><span style="color:red;">'Error In Result'<br />
</span><span style="color:blue;">ELSE </span><span style="color:red;">'No Error'</span><span style="color:green;">--TRY_PARSE('A100.000' AS INT)<br />
</span><span style="color:blue;">END AS </span><span style="color:teal;">ValueInt</span><span style="color:gray;">;<br />
</span></code></p>
<p style="text-align:justify;">2) <strong></strong><a href="http://blog.sqlauthority.com/2011/09/10/sql-server-denali-logical-function-iif-a-quick-introduction/" target="_blank"><strong>IIF()</strong></a> is nothing but a shorthand to CASE statement. Here is a quick example:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:teal;">IIF </span><span style="color:gray;">( -</span><span style="color:teal;">1 </span><span style="color:gray;">&lt; </span><span style="color:teal;">1</span><span style="color:gray;">, </span><span style="color:red;">'TRUE'</span><span style="color:gray;">, </span><span style="color:red;">'FALSE' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Result</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;">Now, IIF is simulated by CASE statement</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">CASE<br />
</span><span style="color:blue;">WHEN </span><span style="color:gray;">-</span><span style="color:black;">1 </span><span style="color:gray;">&lt; </span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:red;">'TRUE'<br />
</span><span style="color:blue;">ELSE </span><span style="color:red;">'FALSE' </span><span style="color:blue;">END AS </span><span style="color:black;">Result</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;">The matter of the fact is that when you see the execution plan of IIF it also shows that internally it is using the CASE statement only.</p>
<p style="text-align:justify;">3) <a href="http://blog.sqlauthority.com/2011/09/11/sql-server-denali-logical-function-choose-a-quick-introduction/" target="_blank"><strong>CHOOSE()</strong></a> is also a shorthand to CASE statement. Here is a quick example:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:teal;">AdventureWorks2008R2<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">A.ModifiedDate</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATEPART</span><span style="color:gray;">(</span><span style="color:teal;">dw</span><span style="color:gray;">, </span><span style="color:teal;">A.ModifiedDate</span><span style="color:gray;">) </span><span style="color:teal;">DayofWeek</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:teal;">dw</span><span style="color:gray;">, </span><span style="color:teal;">A.ModifiedDate</span><span style="color:gray;">) </span><span style="color:teal;">DayofWeek</span><span style="color:gray;">,<br />
</span><span style="color:teal;">CHOOSE</span><span style="color:gray;">(</span><span style="color:magenta;">DATEPART</span><span style="color:gray;">(</span><span style="color:teal;">dw</span><span style="color:gray;">, </span><span style="color:teal;">A.ModifiedDate</span><span style="color:gray;">), </span><span style="color:red;">'WEEKEND'</span><span style="color:gray;">,</span><span style="color:red;">'Weekday'</span><span style="color:gray;">,<br />
</span><span style="color:red;">'Weekday'</span><span style="color:gray;">,</span><span style="color:red;">'Weekday'</span><span style="color:gray;">,</span><span style="color:red;">'Weekday'</span><span style="color:gray;">,</span><span style="color:red;">'Weekday'</span><span style="color:gray;">,</span><span style="color:red;">'WEEKEND'</span><span style="color:gray;">) </span><span style="color:teal;">WorkDay<br />
</span><span style="color:blue;">FROM </span><span style="color:teal;">[Person].[Address] A<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:teal;">[Person].[StateProvince] SP<br />
</span><span style="color:blue;">ON </span><span style="color:teal;">SP.StateProvinceID </span><span style="color:blue;">= </span><span style="color:teal;">A.StateProvinceID<br />
GO</span></code></p>
<p style="text-align:justify;">The above statement is re-written using the CASE statement.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:teal;">AdventureWorks2008R2<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">A.ModifiedDate</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATEPART</span><span style="color:gray;">(</span><span style="color:teal;">dw</span><span style="color:gray;">, </span><span style="color:teal;">A.ModifiedDate</span><span style="color:gray;">) </span><span style="color:teal;">DayofWeek</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:teal;">dw</span><span style="color:gray;">, </span><span style="color:teal;">A.ModifiedDate</span><span style="color:gray;">) </span><span style="color:teal;">DayofWeek</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE DATEPART</span><span style="color:gray;">(</span><span style="color:teal;">dw</span><span style="color:gray;">, </span><span style="color:teal;">A.ModifiedDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:teal;">1 </span><span style="color:blue;">THEN </span><span style="color:red;">'WEEKEND'<br />
</span><span style="color:blue;">WHEN </span><span style="color:teal;">2 </span><span style="color:blue;">THEN </span><span style="color:red;">'Weekday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:teal;">3 </span><span style="color:blue;">THEN </span><span style="color:red;">'Weekday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:teal;">4 </span><span style="color:blue;">THEN </span><span style="color:red;">'Weekday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:teal;">5 </span><span style="color:blue;">THEN </span><span style="color:red;">'Weekday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:teal;">6 </span><span style="color:blue;">THEN </span><span style="color:red;">'Weekday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:teal;">7 </span><span style="color:blue;">THEN </span><span style="color:red;">'WEEKEND'<br />
</span><span style="color:blue;">END  </span><span style="color:teal;">WorkDay<br />
</span><span style="color:blue;">FROM </span><span style="color:teal;">[Person].[Address] A<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:teal;">[Person].[StateProvince] SP<br />
</span><span style="color:blue;">ON </span><span style="color:teal;">SP.StateProvinceID </span><span style="color:blue;">= </span><span style="color:teal;">A.StateProvinceID<br />
GO</span></code></p>
<p style="text-align:justify;">Both the above queries are giving us the same result; however, by using CHOOSE function, T-SQL looks much cleaner and easier to read. You can refer <a href="http://blog.sqlauthority.com/2011/09/11/sql-server-denali-logical-function-choose-a-quick-introduction/" target="_blank"><strong>my earlier blog post</strong></a> where I have discussed how CHOOSE() uses the CASE statement under the hood.</p>
<p style="text-align:justify;"><strong><em>Summary:</em></strong> Well, the CASE statement is going nowhere; I think the CASE statement is so important in <strong><em>data presentation</em></strong> that it will be always there, and many new commands will show up and they will be using the CASE statement under the hood.</p>
<p style="text-align:justify;">Now, here is a question back to you &#8211; <strong><em>Does PIVOT use CASE statement too?</em></strong> Please leave your answer as comments.</p>
<p style="text-align:justify;">Reference:  <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/14734/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/14734/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/14734/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/14734/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/14734/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/14734/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/14734/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/14734/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/14734/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/14734/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/14734/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/14734/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/14734/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/14734/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14734&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/09/13/sql-server-denali-new-functions-and-shorthand-for-case-statement-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Denali &#8211; Logical Function &#8211; CHOOSE() &#8211; A Quick Introduction</title>
		<link>http://blog.sqlauthority.com/2011/09/11/sql-server-denali-logical-function-choose-a-quick-introduction/</link>
		<comments>http://blog.sqlauthority.com/2011/09/11/sql-server-denali-logical-function-choose-a-quick-introduction/#comments</comments>
		<pubDate>Sun, 11 Sep 2011 01:30:16 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=14327</guid>
		<description><![CDATA[In SQL Server Denali, there are two new logical functions being introduced, namely: IIF() CHOOSE() Today we will quickly take a look at the CHOOSE() function. This function is very simple and it returns specified index from a list of values. If Index is numeric, it is converted to integer. On the other hand, if [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14327&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">In SQL Server Denali, there are two new logical functions being introduced, namely:</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2011/09/10/sql-server-denali-logical-function-iif-a-quick-introduction/" target="_blank">IIF()</a><br />
<a href="http://blog.sqlauthority.com/2011/09/11/sql-server-denali-logical-function-choose-a-quick-introduction/" target="_blank"><strong>CHOOSE()</strong></a></p>
<p style="text-align:justify;">Today we will quickly take a look at the CHOOSE() function. This function is very simple and it returns specified index from a list of values. If Index is numeric, it is converted to integer. On the other hand, if index is greater than the element in the list, it returns NULL.</p>
<p style="text-align:justify;">Now let us look at these examples showing how CHOOSE() works:</p>
<p style="text-align:justify;"><strong>Example 1: CHOOSE Usage<br />
</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:teal;">CHOOSE </span><span style="color:gray;">( </span><span style="color:teal;">0</span><span style="color:gray;">, </span><span style="color:red;">'TRUE'</span><span style="color:gray;">, </span><span style="color:red;">'FALSE'</span><span style="color:gray;">, </span><span style="color:red;">'Unknown' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Returns_Null</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">CHOOSE </span><span style="color:gray;">( </span><span style="color:teal;">1</span><span style="color:gray;">, </span><span style="color:red;">'TRUE'</span><span style="color:gray;">, </span><span style="color:red;">'FALSE'</span><span style="color:gray;">, </span><span style="color:red;">'Unknown' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Returns_First</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">CHOOSE </span><span style="color:gray;">( </span><span style="color:teal;">2</span><span style="color:gray;">, </span><span style="color:red;">'TRUE'</span><span style="color:gray;">, </span><span style="color:red;">'FALSE'</span><span style="color:gray;">, </span><span style="color:red;">'Unknown' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Returns_Second</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">CHOOSE </span><span style="color:gray;">( </span><span style="color:teal;">3</span><span style="color:gray;">, </span><span style="color:red;">'TRUE'</span><span style="color:gray;">, </span><span style="color:red;">'FALSE'</span><span style="color:gray;">, </span><span style="color:red;">'Unknown' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Returns_Third</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">CHOOSE </span><span style="color:gray;">( </span><span style="color:teal;">4</span><span style="color:gray;">, </span><span style="color:red;">'TRUE'</span><span style="color:gray;">, </span><span style="color:red;">'FALSE'</span><span style="color:gray;">, </span><span style="color:red;">'Unknown' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Result_NULL</span><span style="color:gray;">;<br />
</span></code></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:gray;"><img class="alignnone" src="http://www.pinaldave.com/bimg/choose.jpg" alt="" width="502" height="390" /></span></code></p>
<p style="text-align:justify;">You can see that when index is Zero or greater than the elements in the list,  it returns the value as NULL and it does not return error.</p>
<p style="text-align:justify;"><strong>Example 2: Usage of CHOOSE when Index is Not Integer</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:teal;">CHOOSE </span><span style="color:gray;">( </span><span style="color:teal;">1.1</span><span style="color:gray;">, </span><span style="color:red;">'TRUE'</span><span style="color:gray;">, </span><span style="color:red;">'FALSE'</span><span style="color:gray;">, </span><span style="color:red;">'Unknown' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Returns_First</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">CHOOSE </span><span style="color:gray;">( </span><span style="color:teal;">2.9</span><span style="color:gray;">, </span><span style="color:red;">'TRUE'</span><span style="color:gray;">, </span><span style="color:red;">'FALSE'</span><span style="color:gray;">, </span><span style="color:red;">'Unknown' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Returns_Second</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/choose1.jpg" alt="" width="499" height="168" /></p>
<p style="text-align:justify;">You can see that Float value is automatically converted to Integer value and appropriate list value is selected.</p>
<p style="text-align:justify;"><strong>Example 3: Usage of CHOOSE with Table</strong></p>
<p style="text-align:justify;">CHOOSE() function can be very useful when it is used in the table as well. In the following example, I am trying to figure out if the day is weekend or weekday using CHOOSE function. There are other ways to figure that out as well, but here in this example I am using that to demonstrate the usage of the CHOOSE function.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:teal;">AdventureWorks2008R2<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">A.ModifiedDate</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATEPART</span><span style="color:gray;">(</span><span style="color:teal;">dw</span><span style="color:gray;">, </span><span style="color:teal;">A.ModifiedDate</span><span style="color:gray;">) </span><span style="color:teal;">DayofWeek</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:teal;">dw</span><span style="color:gray;">, </span><span style="color:teal;">A.ModifiedDate</span><span style="color:gray;">) </span><span style="color:teal;">DayofWeek</span><span style="color:gray;">,<br />
</span><span style="color:teal;">CHOOSE</span><span style="color:gray;">(</span><span style="color:magenta;">DATEPART</span><span style="color:gray;">(</span><span style="color:teal;">dw</span><span style="color:gray;">, </span><span style="color:teal;">A.ModifiedDate</span><span style="color:gray;">), </span><span style="color:red;">'WEEKEND'</span><span style="color:gray;">,</span><span style="color:red;">'Weekday'</span><span style="color:gray;">,<br />
</span><span style="color:red;">'Weekday'</span><span style="color:gray;">,</span><span style="color:red;">'Weekday'</span><span style="color:gray;">,</span><span style="color:red;">'Weekday'</span><span style="color:gray;">,</span><span style="color:red;">'Weekday'</span><span style="color:gray;">,</span><span style="color:red;">'WEEKEND'</span><span style="color:gray;">) </span><span style="color:teal;">WorkDay<br />
</span><span style="color:blue;">FROM </span><span style="color:teal;">[Person].[Address] A<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:teal;">[Person].[StateProvince] SP<br />
</span><span style="color:blue;">ON </span><span style="color:teal;">SP.StateProvinceID </span><span style="color:blue;">= </span><span style="color:teal;">A.StateProvinceID<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">In the following example, we see the result of the query listed above. <code style="font-size:12px;"></code></p>
<p style="text-align:justify;"><a href="http://www.pinaldave.com/bimg/choose2.jpg"><img class="alignnone" src="http://www.pinaldave.com/bimg/choose2.jpg" alt="" width="500" /></a></p>
<p style="text-align:justify;"><strong>Example 4: Usage of CHOOSE with Table and CASE statement and performance comparison</strong><br />
<code style="font-size:12px;"></code></p>
<p style="text-align:justify;">Let us re-write the above query with CASE statement first.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks2008R2<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">A.ModifiedDate</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATEPART</span><span style="color:gray;">(</span><span style="color:black;">dw</span><span style="color:gray;">, </span><span style="color:black;">A.ModifiedDate</span><span style="color:gray;">) </span><span style="color:black;">DayofWeek</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:black;">dw</span><span style="color:gray;">, </span><span style="color:black;">A.ModifiedDate</span><span style="color:gray;">) </span><span style="color:black;">DayofWeek</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE DATEPART</span><span style="color:gray;">(</span><span style="color:black;">dw</span><span style="color:gray;">, </span><span style="color:black;">A.ModifiedDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:red;">'WEEKEND'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">2 </span><span style="color:blue;">THEN </span><span style="color:red;">'Weekday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">3 </span><span style="color:blue;">THEN </span><span style="color:red;">'Weekday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">4 </span><span style="color:blue;">THEN </span><span style="color:red;">'Weekday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">5 </span><span style="color:blue;">THEN </span><span style="color:red;">'Weekday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">6 </span><span style="color:blue;">THEN </span><span style="color:red;">'Weekday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">7 </span><span style="color:blue;">THEN </span><span style="color:red;">'WEEKEND'<br />
</span><span style="color:blue;">END  </span><span style="color:black;">WorkDay<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[Person].[Address] A<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">[Person].[StateProvince] SP<br />
</span><span style="color:blue;">ON </span><span style="color:black;">SP.StateProvinceID </span><span style="color:blue;">= </span><span style="color:black;">A.StateProvinceID<br />
GO</span></code></p>
<p style="text-align:justify;">The following image demonstrates that the results from both the queries are the same.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/choose4.jpg" alt="" width="501" height="707" /></p>
<p style="text-align:justify;">Now let us compare the execution plans of both the query &#8211; the CHOOSE function query and CASE Statement.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/choose5.jpg" alt="" width="503" height="345" /></p>
<p style="text-align:justify;">When the execution plans are compared, it is very clear that the cost of both queries is the same. The execution plans also look similar. Let us now examine the properties of the Compute Scalar function for both execution plans.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/choose6.jpg" alt="" width="417" height="407" /></p>
<p style="text-align:justify;">When the properties of the Compute Scalar function are examined, it is clear that both of them are used at the end CASE statement. Just like IIF function, the CHOOSE function is also the shorthand of the CASE statement.</p>
<p style="text-align:justify;">Reference:<strong></strong>  <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/14327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/14327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/14327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/14327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/14327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/14327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/14327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/14327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/14327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/14327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/14327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/14327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/14327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/14327/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14327&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/09/11/sql-server-denali-logical-function-choose-a-quick-introduction/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/choose.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/choose1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/choose2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/choose4.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/choose5.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/choose6.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Denali &#8211; Logical Function &#8211; IIF() &#8211; A Quick Introduction</title>
		<link>http://blog.sqlauthority.com/2011/09/10/sql-server-denali-logical-function-iif-a-quick-introduction/</link>
		<comments>http://blog.sqlauthority.com/2011/09/10/sql-server-denali-logical-function-iif-a-quick-introduction/#comments</comments>
		<pubDate>Sat, 10 Sep 2011 01:30:39 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=14311</guid>
		<description><![CDATA[In SQL Server Denali, there are two new logical functions being introduced, namely: IIF() CHOOSE() Today, we will have a look at the IIF() function. This function does not need any introduction as developers have used this function in various languages from ages. This function is shorthand way for writing CASE statement. These functions take [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14311&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">In SQL Server Denali, there are two new logical functions being introduced, namely:</p>
<p style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2011/09/10/sql-server-denali-logical-function-iif-a-quick-introduction/" target="_blank">IIF()</a></strong><br />
<a href="http://blog.sqlauthority.com/2011/09/11/sql-server-denali-logical-function-choose-a-quick-introduction/" target="_blank">CHOOSE()</a></p>
<p style="text-align:justify;">Today, we will have a look at the IIF() function. This function does not need any introduction as developers have used this function in various languages from ages. This function is shorthand way for writing CASE statement. These functions take three arguments. If the first argument is true, it will return the second argument as result or it will return the third argument as result.</p>
<p style="text-align:justify;">IIF can be nested as well, which makes its usage very interesting. The limit of nesting of IIF is same as CASE statement, which is capped at 10.</p>
<p style="text-align:justify;">Now, let us look at these examples that show how IIF() works:</p>
<p style="text-align:justify;"><strong>Example 1: IIF Usage<br />
</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:teal;">IIF </span><span style="color:gray;">( -</span><span style="color:teal;">1 </span><span style="color:gray;">&lt; </span><span style="color:teal;">1</span><span style="color:gray;">, </span><span style="color:red;">'TRUE'</span><span style="color:gray;">, </span><span style="color:red;">'FALSE' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">Result</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:gray;"><img class="alignnone" src="http://www.pinaldave.com/bimg/iif1.jpg" alt="" width="514" height="125" /> </span></code></p>
<p style="text-align:justify;"><strong>Example 2: IIF simulated by CASE statement</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">CASE<br />
</span><span style="color:blue;">WHEN </span><span style="color:gray;">-</span><span style="color:teal;">1 </span><span style="color:gray;">&lt; </span><span style="color:teal;">1 </span><span style="color:blue;">THEN </span><span style="color:red;">'TRUE'<br />
</span><span style="color:blue;">ELSE </span><span style="color:red;">'FALSE' </span><span style="color:blue;">END AS </span><span style="color:teal;">Result</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/iif2.jpg" alt="" width="401" height="208" /><code style="font-size:12px;"></code></p>
<p style="text-align:justify;"><strong>Example 3: IIF with NULL<br />
</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">CASE<br />
</span><span style="color:blue;">WHEN </span><span style="color:gray;">-</span><span style="color:teal;">1 </span><span style="color:gray;">&lt; </span><span style="color:teal;">1 </span><span style="color:blue;">THEN </span><span style="color:red;">'TRUE'<br />
</span><span style="color:blue;">ELSE </span><span style="color:red;">'FALSE' </span><span style="color:blue;">END AS </span><span style="color:teal;">Result</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/iif3.jpg" alt="" width="510" height="144" /><code style="font-size:12px;"></code></p>
<p style="text-align:justify;"><strong>Example 4: Nested IIF<br />
</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">IIF </span><span style="color:gray;">( <span style="color:#008080;">-</span></span><span style="color:#008080;">1 &lt; 1</span><span style="color:gray;">, </span><span style="color:black;">IIF </span><span style="color:gray;">( </span><span style="color:#008080;">1=1</span><span style="color:gray;">, </span><span style="color:red;">'Inner True'</span><span style="color:gray;">, </span><span style="color:red;">'Inner False' </span><span style="color:gray;">), </span><span style="color:red;">'FALSE' </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:#008080;">Result</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/iif4.jpg" alt="" width="518" height="177" /><code style="font-size:12px;"></code></p>
<p style="text-align:justify;"><strong>Example 5: IIF used along with TRY_PARSE and Table<br />
</strong><br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:teal;">AdventureWorks2008R2</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">SP.[StateProvinceCode]</span><span style="color:gray;">,<br />
</span><span style="color:teal;">A.[PostalCode]</span><span style="color:gray;">,<br />
</span><span style="color:teal;">IIF</span><span style="color:gray;">(</span><span style="color:teal;">TRY_PARSE</span><span style="color:gray;">(</span><span style="color:teal;">A.[PostalCode] </span><span style="color:blue;">AS INT</span><span style="color:gray;">) </span><span style="color:blue;">IS </span><span style="color:gray;">NULL,<br />
</span><span style="color:red;">'Canada'</span><span style="color:gray;">,</span><span style="color:red;">'United States'</span><span style="color:gray;">) </span><span style="color:teal;">Country<br />
</span><span style="color:blue;">FROM </span><span style="color:teal;">[Person].[Address] A<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:teal;">[Person].[StateProvince] SP<br />
</span><span style="color:blue;">ON </span><span style="color:teal;">SP.StateProvinceID </span><span style="color:blue;">= </span><span style="color:teal;">A.StateProvinceID<br />
GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/iif5.jpg" alt="" width="501" height="343" /><code style="font-size:12px;"></code></p>
<p style="text-align:justify;">In above example, we can see how IIF is used instead of CASE statement and the decision is made during run time using <a href="http://blog.sqlauthority.com/2011/09/07/sql-server-denali-conversion-function-try_parse-a-quick-introduction/" target="_blank"><strong>TRY_PARSE()</strong></a> function. You can read more about the function <a href="http://blog.sqlauthority.com/2011/09/07/sql-server-denali-conversion-function-try_parse-a-quick-introduction/" target="_blank"><strong>TRY_PARSE()</strong></a> here. IIF can be used the same way as CASE statement in SELECT statement.</p>
<p style="text-align:justify;">In the next blog post, we will discuss the difference between CONVERT and PARSE as well as TRY_CONVERT and TRY_PARSE. We will also look at a couple of interesting trivia questions.</p>
<p style="text-align:justify;">Reference:<strong></strong>  Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/14311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/14311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/14311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/14311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/14311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/14311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/14311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/14311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/14311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/14311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/14311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/14311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/14311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/14311/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14311&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/09/10/sql-server-denali-logical-function-iif-a-quick-introduction/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/iif1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/iif2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/iif3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/iif4.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/iif5.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Denali &#8211; Conversion Function &#8211; Difference between PARSE(), TRY_PARSE(), TRY_CONVERT()</title>
		<link>http://blog.sqlauthority.com/2011/09/09/sql-server-denali-conversion-function-difference-between-parse-try_parse-try_convert/</link>
		<comments>http://blog.sqlauthority.com/2011/09/09/sql-server-denali-conversion-function-difference-between-parse-try_parse-try_convert/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 01:30:56 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=14281</guid>
		<description><![CDATA[In SQL Server Denali, three new conversion functions have been introduced, namely, PARSE() TRY_PARSE() TRY_CONVERT() Earlier, we learnt about above three functions in various blog posts. If you have missed it, I suggest that you read all the three following blog posts before continuing this blog post as that will give you better understanding of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14281&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">In SQL Server Denali, three new conversion functions have been introduced, namely,</p>
<p style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2011/09/06/sql-server-denali-conversion-function-parse-a-quick-introduction/" target="_blank">PARSE()</a></strong><br />
<strong> <a href="http://blog.sqlauthority.com/2011/09/07/sql-server-denali-conversion-function-try_parse-a-quick-introduction/" target="_blank">TRY_PARSE()</a></strong><br />
<strong> <a href="http://blog.sqlauthority.com/2011/09/08/sql-server-denali-conversion-function-try_convert-a-quick-introduction/" target="_blank">TRY_CONVERT()</a></strong></p>
<p style="text-align:justify;">Earlier, we learnt about above three functions in various blog posts. If you have missed it, I suggest that you read all the three following blog posts before continuing this blog post as that will give you better understanding of the subject. I have so far received few questions on this subject; so I have attempted to present this blog post in question and answer format.</p>
<p style="text-align:justify;"><strong><em>Q: What is the difference between PARSE() and TRY_PARSE() functions?</em></strong><br />
A: While attempting to PARSE any column, if you get one or more incorrect values, PARSE function will throw an error. However, if you use TRY_PARSE function, it will not throw error but will return the result as NULL.</p>
<p style="text-align:justify;"><strong><em>Q: What is the difference between PARSE() and CONVERT() functions?</em></strong><br />
A: Both are quite different. PARSE function tries to parse a string and returns the value that is parsed from that string, if it can. CONVERT function tries to convert the string to a specified datatype; if it cannot, it will return an error. If you are not sure by what these two statements mean, consider the following example where string &#8217;100.00&#8242; is attempted to PARSE and CONVERT to INT.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:teal;">PARSE</span><span style="color:gray;">(</span><span style="color:red;">'100.000' </span><span style="color:blue;">AS INT</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">PARINT</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:red;">'100.000'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:teal;">CONINT</span><span style="color:gray;">;<br />
</span></code></p>
<p style="text-align:justify;">Let us check the result of the same.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/parint.jpg" alt="" width="458" height="149" /></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/conint.jpg" alt="" /></p>
<p style="text-align:justify;">You can clearly see that CONVERT failed because for it to convert string to a specified datatype, a value that matches the destination datatype is needed, whereas PARSE parsed the string and returned specified datatype-matched string where it can. If you had the specified string value as &#8216;A100.00&#8242; instead of &#8217;100.000,&#8217; both of them would have returned as error as they could not understand how this can be converted to INT.</p>
<p style="text-align:justify;">Additionally, PARSE uses CLR datatype under the hood and currently can only convert a string to INT and DATETIME, whereas CONVERT works with any compatible datatype as well as it can be used to format dates.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/convertexample.jpg" alt="" width="492" height="156" /></p>
<p style="text-align:justify;"><strong><em>Q: I have disabled CLR using sp_configure; how can I still use PARSE function?</em></strong><br />
A: If the user has disabled the CLR by using sp_configure, but .net framework is installed on the server, the PARSE function continues to work since it is a system function and not a user-defined function.</p>
<p style="text-align:justify;"><strong><em>Q: What is optimal PARSE or CONVERT?</em></strong><br />
A: They are different and have different usages. Try both; whichever gives you a correct answer is something that you would want to use.</p>
<p style="text-align:justify;">Just for kicks, I ran the following query together and checked their STATISTICS IO and execution plan; I found both them to be equal.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:teal;">AdventureWorks2008R2<br />
GO<br />
</span><span style="color:blue;">SET STATISTICS  </span><span style="color:teal;">IO </span><span style="color:blue;">ON</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">SP.[StateProvinceCode]<br />
</span><span style="color:gray;">,</span><span style="color:teal;">PARSE</span><span style="color:gray;">(</span><span style="color:teal;">A.[PostalCode] </span><span style="color:blue;">AS INT</span><span style="color:gray;">) </span><span style="color:teal;">PostCode<br />
</span><span style="color:blue;">FROM </span><span style="color:teal;">[Person].[Address] A<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:teal;">[Person].[StateProvince] SP<br />
</span><span style="color:blue;">ON </span><span style="color:teal;">SP.StateProvinceID </span><span style="color:blue;">= </span><span style="color:teal;">A.StateProvinceID<br />
</span><span style="color:blue;">WHERE </span><span style="color:teal;">SP.[CountryRegionCode] </span><span style="color:blue;">= </span><span style="color:red;">'US'<br />
</span><span style="color:gray;">AND </span><span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:teal;">A.[PostalCode]</span><span style="color:gray;">) &lt;=</span><span style="color:teal;">6<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:teal;">SP.[StateProvinceCode]<br />
</span><span style="color:gray;">,</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">INT</span><span style="color:gray;">,</span><span style="color:teal;">A.[PostalCode]</span><span style="color:gray;">) </span><span style="color:teal;">PostCode<br />
</span><span style="color:blue;">FROM </span><span style="color:teal;">[Person].[Address] A<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:teal;">[Person].[StateProvince] SP<br />
</span><span style="color:blue;">ON </span><span style="color:teal;">SP.StateProvinceID </span><span style="color:blue;">= </span><span style="color:teal;">A.StateProvinceID<br />
</span><span style="color:blue;">WHERE </span><span style="color:teal;">SP.[CountryRegionCode] </span><span style="color:blue;">= </span><span style="color:red;">'US'<br />
</span><span style="color:gray;">AND </span><span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:teal;">A.[PostalCode]</span><span style="color:gray;">) &lt;=</span><span style="color:teal;">6<br />
GO<br />
</span></code><br />
The output of STATISTICS IO is as following: In both case, the same amount IO reads are happening.</p>
<pre>(8666 row(s) affected)
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Address'. Scan count 1, logical reads 216, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'StateProvince'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(8666 row(s) affected)
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Address'. Scan count 1, logical reads 216, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'StateProvince'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.</pre>
<p style="text-align:justify;">Let us check the execution plan; it looks same as well.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/conpar.jpg" alt="" width="500" height="308" /></p>
<p style="text-align:justify;">Let me know if you have any other question; I will expand this article appropriately.</p>
<p style="text-align:justify;">Reference:<strong></strong> <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/14281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/14281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/14281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/14281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/14281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/14281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/14281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/14281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/14281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/14281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/14281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/14281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/14281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/14281/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=14281&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/09/09/sql-server-denali-conversion-function-difference-between-parse-try_parse-try_convert/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/parint.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/conint.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/convertexample.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/conpar.jpg" medium="image" />
	</item>
	</channel>
</rss>
