<?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 DateTime</title>
	<atom:link href="http://blog.sqlauthority.com/category/sql-datetime/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 DateTime</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; DATEDIFF &#8211; Accuracy of Various Dateparts</title>
		<link>http://blog.sqlauthority.com/2011/10/21/sql-server-datediff-accuracy-of-various-dateparts/</link>
		<comments>http://blog.sqlauthority.com/2011/10/21/sql-server-datediff-accuracy-of-various-dateparts/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 01:30:11 +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 DateTime]]></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=15110</guid>
		<description><![CDATA[I recently received the following question through email and I found it very interesting so I want to share it with you. &#8220;Hi Pinal, In SQL statement below the time difference between two given dates is 3 sec, but when checked in terms of Min it says 1 Min (whereas the actual min is 0.05Min) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15110&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I recently received the following question through email and I found it very interesting so I want to share it with you.</p>
<p style="padding-left:30px;text-align:justify;">&#8220;Hi Pinal,</p>
<p style="padding-left:30px;text-align:justify;">In SQL statement below the time difference between two given dates is 3 sec, but when checked in terms of Min it says 1 Min <strong>(whereas the actual min is 0.05Min)</strong></p>
<p style="padding-left:30px;text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">MI</span><span style="color:gray;">,</span><span style="color:red;">'2011-10-14 02:18:58' </span><span style="color:gray;">, </span><span style="color:red;">'2011-10-14 02:19:01'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">MIN_DIFF</span></code></p>
<p style="padding-left:30px;text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/mindiff.PNG" alt="" width="500" /><code style="font-size:12px;"><span style="color:black;"><br />
</span></code></p>
<p style="padding-left:30px;text-align:justify;">Is this is a BUG in SQL Server ?&#8221;</p>
<p style="text-align:justify;">Answer is <strong>NO</strong>.</p>
<p style="text-align:justify;">It is not a bug; it is a feature that works like that. Let us understand that in a bit more detail. When you instruct SQL Server to find the time difference in minutes, it just looks at the minute section only and completely ignores hour, second, millisecond, etc. So in terms of difference in minutes, it is indeed 1.</p>
<p style="text-align:justify;">The following will also clear how DATEDIFF works:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:magenta;">YEAR</span><span style="color:gray;">,</span><span style="color:red;">'2011-12-31 23:59:59' </span><span style="color:gray;">, </span><span style="color:red;">'2012-01-01 00:00:00'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">YEAR_DIFF</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/mindiff1.PNG" alt="" width="500" /><code style="font-size:12px;"></code></p>
<p style="text-align:justify;">The difference between the above dates is just 1 second, but in terms of year difference it shows 1.</p>
<p style="text-align:justify;">If you want to have accuracy in seconds, you need to use a different approach. In the first example, the accurate method is to find the number of seconds first and then divide it by 60 to convert it to minutes.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">second</span><span style="color:gray;">,</span><span style="color:red;">'2011-10-14 02:18:58' </span><span style="color:gray;">, </span><span style="color:red;">'2011-10-14 02:19:01'</span><span style="color:gray;">)/</span><span style="color:black;">60.0 </span><span style="color:blue;">AS </span><span style="color:black;">MIN_DIFF</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/mindiff2.PNG" alt="" width="500" /></p>
<p style="text-align:justify;">Even though the concept is very simple it is always a good idea to refresh it. Please share your related experience with me through your comments.<br />
<code style="font-size:12px;"></code></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-datetime/'>SQL DateTime</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/15110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15110/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15110&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/10/21/sql-server-datediff-accuracy-of-various-dateparts/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/mindiff.PNG" medium="image" />

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

		<media:content url="http://www.pinaldave.com/bimg/mindiff2.PNG" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Information Related to DATETIME and DATETIME2</title>
		<link>http://blog.sqlauthority.com/2010/11/15/sql-server-information-related-to-datetime-and-datetime2/</link>
		<comments>http://blog.sqlauthority.com/2010/11/15/sql-server-information-related-to-datetime-and-datetime2/#comments</comments>
		<pubDate>Mon, 15 Nov 2010 01:30:31 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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=10876</guid>
		<description><![CDATA[I recently received interesting comment on the blog regarding workaround to overcome the precision issue while dealing with DATETIME and DATETIME2. I have written over this subject earlier over here. SQL SERVER – Difference Between GETDATE and SYSDATETIME SQL SERVER – Difference Between DATETIME and DATETIME2 – WITH GETDATE SQL SERVER – Difference Between DATETIME [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10876&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I recently received interesting comment on the blog regarding workaround to overcome the precision issue while dealing with DATETIME and DATETIME2.</p>
<p style="text-align:justify;">I have written over this subject earlier over here.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2010/06/10/sql-server-difference-between-getdate-and-sysdatetime/" target="_blank">SQL SERVER – Difference Between GETDATE and SYSDATETIME</a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2010/06/12/sql-server-difference-between-datetime-and-datetime2-with-getdate/" target="_blank">SQL SERVER – Difference Between DATETIME and DATETIME2 – WITH GETDATE</a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2010/06/11/sql-server-difference-between-datetime-and-datetime2/" target="_blank">SQL SERVER – Difference Between DATETIME and DATETIME2</a></p>
<p style="text-align:justify;">SQL Expert Jing Sheng Zhong has left following comment:</p>
<p style="text-align:justify;">The issue you found in SQL server new datetime type is related time source function precision. Folks have found the root reason of the problem &#8211; when data time values are converted (implicit or explicit) between different data type, which would lose some precision, so the result cannot match each other as thought. Here I would like to gave a work around solution to solve the problem which the developers met.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Declare and loop<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Intveral </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:#434343;">@CurDate </span><span style="color:black;">DATETIMEOFFSET</span><span style="color:gray;">;<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:#434343;">#TimeTable </span><span style="color:gray;">(</span><span style="color:black;">FirstDate </span><span style="color:blue;">DATETIME</span><span style="color:gray;">, </span><span style="color:black;">LastDate DATETIME2</span><span style="color:gray;">, </span><span style="color:black;">GlobalDate DATETIMEOFFSET</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Intveral </span><span style="color:blue;">= </span><span style="color:black;">10000<br />
</span><span style="color:blue;">WHILE </span><span style="color:gray;">(</span><span style="color:#434343;">@Intveral </span><span style="color:gray;">&gt; </span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">BEGIN<br />
</span><span style="color:green;">----SET @CurDate = SYSDATETIMEOFFSET(); -- higher precision for future use only<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@CurDate </span><span style="color:blue;">= </span><span style="color:black;">TODATETIMEOFFSET</span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(),</span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">N</span><span style="color:gray;">,</span><span style="color:magenta;">GETUTCDATE</span><span style="color:gray;">(),</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">())); </span><span style="color:green;">-- lower precision to match exited date process<br />
</span><span style="color:blue;">INSERT </span><span style="color:#434343;">#TimeTable </span><span style="color:gray;">(</span><span style="color:black;">FirstDate</span><span style="color:gray;">, </span><span style="color:black;">LastDate</span><span style="color:gray;">, </span><span style="color:black;">GlobalDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:#434343;">@CurDate</span><span style="color:gray;">, </span><span style="color:#434343;">@CurDate</span><span style="color:gray;">, </span><span style="color:#434343;">@CurDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Intveral </span><span style="color:blue;">= </span><span style="color:#434343;">@Intveral </span><span style="color:gray;">- </span><span style="color:black;">1<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Distinct Values<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(</span><span style="color:blue;">DISTINCT </span><span style="color:black;">FirstDate</span><span style="color:gray;">) </span><span style="color:black;">D_DATETIME</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">COUNT</span><span style="color:gray;">(</span><span style="color:blue;">DISTINCT </span><span style="color:black;">LastDate</span><span style="color:gray;">) </span><span style="color:black;">D_DATETIME2</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">COUNT</span><span style="color:gray;">(</span><span style="color:blue;">DISTINCT </span><span style="color:black;">GlobalDate</span><span style="color:gray;">) </span><span style="color:black;">D_SYSGETDATE<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">#TimeTable<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Join<br />
</span><span style="color:blue;">SELECT DISTINCT </span><span style="color:black;">a.FirstDate</span><span style="color:gray;">,</span><span style="color:black;">b.LastDate</span><span style="color:gray;">, </span><span style="color:black;">b.GlobalDate</span><span style="color:gray;">, </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:black;">b.GlobalDate </span><span style="color:blue;">AS DATETIME</span><span style="color:gray;">) </span><span style="color:black;">GlobalDateASDateTime<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">#TimeTable </span><span style="color:black;">a<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:#434343;">#TimeTable </span><span style="color:black;">b </span><span style="color:blue;">ON </span><span style="color:black;">a.FirstDate </span><span style="color:blue;">= </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:black;">b.GlobalDate </span><span style="color:blue;">AS DATETIME</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Select<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">#TimeTable<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Clean up<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:#434343;">#TimeTable<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">If you read my blog <a href="http://blog.sqlauthority.com/2010/06/11/sql-server-difference-between-datetime-and-datetime2/" target="_blank">SQL SERVER – Difference Between DATETIME and DATETIME2</a> you will notice that I have achieved the same using GETDATE().</p>
<p style="text-align:justify;">Are you using DATETIME2 in your production environment? If yes, I am interested to know the use case.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://www.sqlauthority.com/">http://www.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/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-datetime/'>SQL DateTime</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/10876/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10876/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10876/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10876/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10876/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10876/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10876/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10876/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10876/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10876/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10876/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10876/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10876/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10876/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10876&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/11/15/sql-server-information-related-to-datetime-and-datetime2/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>
	</item>
		<item>
		<title>SQL SERVER &#8211; Datetime Function TODATETIMEOFFSET Example</title>
		<link>http://blog.sqlauthority.com/2010/07/16/sql-server-datetime-function-todatetimeoffset-example/</link>
		<comments>http://blog.sqlauthority.com/2010/07/16/sql-server-datetime-function-todatetimeoffset-example/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 01:30:18 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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=9575</guid>
		<description><![CDATA[Earlier I wrote about SQL SERVER – Datetime Function SWITCHOFFSET Example. After reading this blog post, I got another quick reply that if I can explain the usage of TODATETIMEOFFSET as well. Let us go over the definition of the TODATETIMEOFFSET  from BOL: Returns a datetimeoffset value that is translated from a datetime2 expression. What [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9575&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Earlier I wrote about <strong><a href="http://blog.sqlauthority.com/2010/07/15/sql-server-datetime-function-switchoffset-example/" target="_blank">SQL SERVER – Datetime Function SWITCHOFFSET Example</a></strong>. After reading this blog post, I got another quick reply that if I can explain the usage of TODATETIMEOFFSET as well.</p>
<p style="text-align:justify;">Let us go over the definition of the TODATETIMEOFFSET  from <a href="http://msdn.microsoft.com/en-us/library/bb630335.aspx" target="_blank">BOL</a>: Returns a datetimeoffset value  that is translated from a datetime2  expression.</p>
<p style="text-align:justify;">What essentially it does is that changes the current offset only  offset which we defined. Let us see the example of the  same.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">() </span><span style="color:black;">GetCurrentOffSet</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">TODATETIMEOFFSET</span><span style="color:gray;">(</span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">(), </span><span style="color:red;">'-04:00'</span><span style="color:gray;">) </span><span style="color:red;">'GetCurrentOffSet-4'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">TODATETIMEOFFSET</span><span style="color:gray;">(</span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">(), </span><span style="color:red;">'-02:00'</span><span style="color:gray;">) </span><span style="color:red;">'GetCurrentOffSet-2'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">TODATETIMEOFFSET</span><span style="color:gray;">(</span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">(), </span><span style="color:red;">'+00:00'</span><span style="color:gray;">) </span><span style="color:red;">'GetCurrentOffSet+0'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">TODATETIMEOFFSET</span><span style="color:gray;">(</span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">(), </span><span style="color:red;">'+02:00'</span><span style="color:gray;">) </span><span style="color:red;">'GetCurrentOffSet+2'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">TODATETIMEOFFSET</span><span style="color:gray;">(</span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">(), </span><span style="color:red;">'+04:00'</span><span style="color:gray;">) </span><span style="color:red;">'GetCurrentOffSet+4'</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;">Let us see the resultset below.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/TODATETIMEOFFSET.jpg" alt="" width="383" height="549" /></p>
<p style="text-align:justify;">It is quite clear from the example that only offset changes in the time but unlike <strong><a href="http://blog.sqlauthority.com/2010/07/15/sql-server-datetime-function-switchoffset-example/" target="_blank">SWITCHOFFSET</a></strong> the datetime remains the same.</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/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-datetime/'>SQL DateTime</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/9575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9575/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9575/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9575/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9575/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9575/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9575/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9575/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9575&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/07/16/sql-server-datetime-function-todatetimeoffset-example/feed/</wfw:commentRss>
		<slash:comments>5</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/TODATETIMEOFFSET.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Datetime Function SWITCHOFFSET Example</title>
		<link>http://blog.sqlauthority.com/2010/07/15/sql-server-datetime-function-switchoffset-example/</link>
		<comments>http://blog.sqlauthority.com/2010/07/15/sql-server-datetime-function-switchoffset-example/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 01:30:13 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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=9569</guid>
		<description><![CDATA[I was recently asked if I know how SWITCHOFFSET works. This feature only works in SQL Server 2008. Here is quick definition of the same from BOL: Returns a datetimeoffset value that is changed from the stored time zone offset to a specified new time zone offset. What essentially it does is that changes the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9569&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I was recently asked if I know how SWITCHOFFSET works. This feature only works in SQL Server 2008.</p>
<p style="text-align:justify;">Here is quick definition of the same from <a href="http://msdn.microsoft.com/en-us/library/bb677244.aspx" target="_blank">BOL</a>: Returns a datetimeoffset value  that is changed from the stored time zone offset to a specified new time  zone offset.</p>
<p style="text-align:justify;">What essentially it does is that changes the current offset of the time to any other offset which we defined. Let us see the example of the same.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">() </span><span style="color:black;">GetCurrentOffSet</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SWITCHOFFSET</span><span style="color:gray;">(</span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">(), </span><span style="color:red;">'-04:00'</span><span style="color:gray;">) </span><span style="color:red;">'GetCurrentOffSet-4'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SWITCHOFFSET</span><span style="color:gray;">(</span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">(), </span><span style="color:red;">'-02:00'</span><span style="color:gray;">) </span><span style="color:red;">'GetCurrentOffSet-2'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SWITCHOFFSET</span><span style="color:gray;">(</span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">(), </span><span style="color:red;">'+00:00'</span><span style="color:gray;">) </span><span style="color:red;">'GetCurrentOffSet+0'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SWITCHOFFSET</span><span style="color:gray;">(</span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">(), </span><span style="color:red;">'+02:00'</span><span style="color:gray;">) </span><span style="color:red;">'GetCurrentOffSet+2'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SWITCHOFFSET</span><span style="color:gray;">(</span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">(), </span><span style="color:red;">'+04:00'</span><span style="color:gray;">) </span><span style="color:red;">'GetCurrentOffSet+4'</span><span style="color:gray;">;</span></code></p>
<p style="text-align:justify;">Now let us check the resultset of the same.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/switchoffset.jpg" alt="" width="400" height="573" /></p>
<p style="text-align:justify;">The example above clearly shows that Switch offset does not only change the offset; it also alters the current time. If you look at GetcurrentOffset, it is +5.30; but, you will notice that GetCurrentOffset-2 does not only change the offset to -2. It also changes the time with the appropriate time at Timezone -2.</p>
<p style="text-align:justify;">I suggest that you run the code in SSMS Query Window and observe the code behavior.</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/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-datetime/'>SQL DateTime</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/9569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9569/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9569/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9569/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9569/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9569/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9569/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9569/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9569&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/07/15/sql-server-datetime-function-switchoffset-example/feed/</wfw:commentRss>
		<slash:comments>3</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/switchoffset.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Difference Between DATETIME and DATETIME2 &#8211; WITH GETDATE</title>
		<link>http://blog.sqlauthority.com/2010/06/12/sql-server-difference-between-datetime-and-datetime2-with-getdate/</link>
		<comments>http://blog.sqlauthority.com/2010/06/12/sql-server-difference-between-datetime-and-datetime2-with-getdate/#comments</comments>
		<pubDate>Sat, 12 Jun 2010 01:30:45 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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=9227</guid>
		<description><![CDATA[Earlier I wrote blog post SQL SERVER – Difference Between GETDATE and SYSDATETIME which inspired me to write SQL SERVER &#8211; Difference Between DATETIME and DATETIME2. Now earlier two blog post inspired me to write this blog post (and 4 emails and 3 reads from readers). I previously populated DATETIME and DATETIME2 field with SYSDATETIME, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9227&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Earlier I wrote blog post <strong><a href="http://blog.sqlauthority.com/2010/06/10/sql-server-difference-between-getdate-and-sysdatetime/" target="_blank">SQL SERVER – Difference Between GETDATE and SYSDATETIME</a></strong> which inspired me to write <strong><a href="http://blog.sqlauthority.com/2010/06/11/sql-server-difference-between-datetime-and-datetime2/" target="_blank">SQL SERVER &#8211; Difference Between DATETIME and DATETIME2</a></strong>. Now earlier two blog post inspired me to write this blog post (and 4 emails and 3 reads from readers).</p>
<p style="text-align:justify;">I previously populated DATETIME and DATETIME2 field with SYSDATETIME, which gave me very different behavior as SYSDATETIME was rounded up/down for the DATETIME datatype. I just ran the same experiment but instead of populating SYSDATETIME in this script I will be using GETDATE function.<br />
<code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Intveral </span><span style="color:blue;">INT<br />
SET </span><span style="color:#434343;">@Intveral </span><span style="color:blue;">= </span><span style="color:black;">10000<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:#434343;">#TimeTable </span><span style="color:gray;">(</span><span style="color:black;">FirstDate DATETIME</span><span style="color:gray;">, </span><span style="color:black;">LastDate DATETIME2</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHILE </span><span style="color:gray;">(</span><span style="color:#434343;">@Intveral </span><span style="color:gray;">&gt; </span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">BEGIN<br />
INSERT </span><span style="color:#434343;">#TimeTable </span><span style="color:gray;">(</span><span style="color:black;">FirstDate</span><span style="color:gray;">, </span><span style="color:black;">LastDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(), </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">())<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Intveral </span><span style="color:blue;">= </span><span style="color:#434343;">@Intveral </span><span style="color:gray;">- </span><span style="color:black;">1<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(</span><span style="color:blue;">DISTINCT </span><span style="color:black;">FirstDate</span><span style="color:gray;">) </span><span style="color:black;">D_FirstDate</span><span style="color:gray;">, </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(</span><span style="color:blue;">DISTINCT </span><span style="color:black;">LastDate</span><span style="color:gray;">) </span><span style="color:black;">D_LastDate<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">#TimeTable<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT DISTINCT </span><span style="color:black;">a.FirstDate</span><span style="color:gray;">, </span><span style="color:black;">b.LastDate<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">#TimeTable </span><span style="color:black;">a<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:#434343;">#TimeTable </span><span style="color:black;">b </span><span style="color:blue;">ON </span><span style="color:black;">a.FirstDate </span><span style="color:blue;">= </span><span style="color:black;">b.LastDate<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">#TimeTable<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:#434343;">#TimeTable<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">Let us run above script and observe the results.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/getdatematch.jpg" alt="" width="499" height="544" /></p>
<p style="text-align:justify;">You will find that the values of GETDATE which is populated in both the columns FirstDate and LastDate are very much same. This is because GETDATE is of datatype DATETIME and the precision of the GETDATE is smaller than DATETIME2 there is no rounding happening.</p>
<p style="text-align:justify;">In other word, this experiment is <em><strong>pointless</strong></em>. I have included this as I got 4 emails and 3 twitter questions on this subject. If your datatype of variable is smaller than column datatype there is no manipulation of data, if data type of variable is larger than column datatype the data is rounded.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://www.sqlauthority.com/">http://www.SQLAuthority.com</a>)</strong></p>
<p style="text-align:justify;">
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</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-datetime/'>SQL DateTime</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/9227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9227/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9227&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/06/12/sql-server-difference-between-datetime-and-datetime2-with-getdate/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/getdatematch.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Difference Between DATETIME and DATETIME2</title>
		<link>http://blog.sqlauthority.com/2010/06/11/sql-server-difference-between-datetime-and-datetime2/</link>
		<comments>http://blog.sqlauthority.com/2010/06/11/sql-server-difference-between-datetime-and-datetime2/#comments</comments>
		<pubDate>Fri, 11 Jun 2010 01:30:55 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></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=9217</guid>
		<description><![CDATA[Yesterday I have written a very quick blog post on SQL SERVER – Difference Between GETDATE and SYSDATETIME and I got tremendous response for the same. I suggest you read that blog post before continuing with this blog post today. I had asked people to honestly take part and share their view about the above [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9217&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Yesterday I have written a very quick blog post on <a href="http://blog.sqlauthority.com/2010/06/10/sql-server-difference-between-getdate-and-sysdatetime/" target="_blank"><strong>SQL SERVER – Difference Between GETDATE and SYSDATETIME</strong></a> and I got tremendous response for the same. I suggest you read that blog post before continuing with this blog post today. I had asked people to honestly take part and share their view about the above two system functions.</p>
<p style="text-align:justify;">There are few emails as well as few comments on the blog post asking a question on how did I come to know the difference between the same. The answer is from real world issues. I was called in for performance tuning consultancy, where I was asked a very strange question by one developer. Here is the situation faced by him.</p>
<p style="text-align:justify;">System had a single table with two different columns of datetime. One column was datelastmodified and the second column was datefirstmodified. One of the columns was DATETIME and  the other was DATETIME2. Developer was populating each of them with SYSDATETIME. He assumed that the value inserted in the table will be the same. This table was only accessed by INSERT statement, and there were no updates done over it in application. One fine day, he ran distinct on both these columns and was surprised by the result. He always thought that both the tables will have the same data, but in fact, they had very different data.</p>
<p style="text-align:justify;">He presented this scenario to me. I said this is not possible, but I had to agree with him when I looked at the resultset. Here is the simple script to demonstrate the problem he was facing. This is just a sample of the original table.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Intveral </span><span style="color:blue;">INT<br />
SET </span><span style="color:#434343;">@Intveral </span><span style="color:blue;">= </span><span style="color:black;">10000<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:#434343;">#TimeTable </span><span style="color:gray;">(</span><span style="color:black;">FirstDate DATETIME</span><span style="color:gray;">, </span><span style="color:black;">LastDate DATETIME2</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHILE </span><span style="color:gray;">(</span><span style="color:#434343;">@Intveral </span><span style="color:gray;">&gt; </span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">BEGIN<br />
INSERT </span><span style="color:#434343;">#TimeTable </span><span style="color:gray;">(</span><span style="color:black;">FirstDate</span><span style="color:gray;">, </span><span style="color:black;">LastDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">SYSDATETIME</span><span style="color:gray;">(), </span><span style="color:black;">SYSDATETIME</span><span style="color:gray;">())<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Intveral </span><span style="color:blue;">= </span><span style="color:#434343;">@Intveral </span><span style="color:gray;">- </span><span style="color:black;">1<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(</span><span style="color:blue;">DISTINCT </span><span style="color:black;">FirstDate</span><span style="color:gray;">) </span><span style="color:black;">D_GETDATE</span><span style="color:gray;">, </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(</span><span style="color:blue;">DISTINCT </span><span style="color:black;">LastDate</span><span style="color:gray;">) </span><span style="color:black;">D_SYSGETDATE<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">#TimeTable<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT DISTINCT </span><span style="color:black;">a.FirstDate</span><span style="color:gray;">, </span><span style="color:black;">b.LastDate<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">#TimeTable </span><span style="color:black;">a<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:#434343;">#TimeTable </span><span style="color:black;">b </span><span style="color:blue;">ON </span><span style="color:black;">a.FirstDate </span><span style="color:blue;">= </span><span style="color:black;">b.LastDate<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">#TimeTable<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:#434343;">#TimeTable<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">Let us see the resultset.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/sysget.jpg" alt="" width="476" height="456" /></p>
<p style="text-align:justify;">You can clearly see from result that SYSDATETIME() does not populate the same value in both the fields. In fact, the value is either rounded down or rounded up in the field which is DATETIME. Eventhough we are populating the same value, the values are totally different in both the columns, thus resulting in the SELF JOIN fail and displaying of different DISTINCT values.</p>
<p style="text-align:justify;">The best way is to use GETDATE() if you are using DATETIME, and SYSDATETIME() if you are using DATETIME2 to populate them with current date and time for accurately addressing the precision. As DATETIME2 is introduced in SQL Server 2008, the above script will only work with SQL Server 2008 and later versions.</p>
<p style="text-align:justify;">I hope I have answered some of the questions asked yesterday.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://www.sqlauthority.com/">http://www.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/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-datetime/'>SQL DateTime</a>, <a href='http://blog.sqlauthority.com/category/sql-optimization/'>SQL Optimization</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</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/9217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9217/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9217/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9217/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9217&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/06/11/sql-server-difference-between-datetime-and-datetime2/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/sysget.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Difference Between GETDATE and SYSDATETIME</title>
		<link>http://blog.sqlauthority.com/2010/06/10/sql-server-difference-between-getdate-and-sysdatetime/</link>
		<comments>http://blog.sqlauthority.com/2010/06/10/sql-server-difference-between-getdate-and-sysdatetime/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 01:30:55 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=9213</guid>
		<description><![CDATA[Sometime something so simple skips our mind. I never knew the difference between GETDATE and SYSDATETIME. I just ran simple query as following and realized the difference. SELECT GETDATE() fn_GetDate, SYSDATETIME() fn_SysDateTime In case of GETDATE the precision is till miliseconds and in case of SYSDATETIME the precision is till nanoseconds. Now the questions is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9213&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Sometime something so simple skips our mind. I never knew the difference between GETDATE and SYSDATETIME. I just ran simple query as following and realized the difference.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:darkred;">fn_GetDate</span><span style="color:gray;">, </span><span style="color:black;">SYSDATETIME</span><span style="color:gray;">() </span><span style="color:darkred;">fn_SysDateTime</span></code></p>
<p style="text-align:justify;">In case of GETDATE the precision is till miliseconds and in case of SYSDATETIME the precision is till nanoseconds.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/getdate_sysdatetime.jpg" alt="" width="351" height="50" /></p>
<p style="text-align:justify;">Now the questions is to you &#8211; did you know this? Be honest and please share your views. I already accepted that I did not know this in very first line.</p>
<p style="text-align:justify;">This applies to SQL Server 2008 only.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://www.sqlauthority.com/">http://www.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/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-datetime/'>SQL DateTime</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/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/9213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9213/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9213&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/06/10/sql-server-difference-between-getdate-and-sysdatetime/feed/</wfw:commentRss>
		<slash:comments>32</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/getdate_sysdatetime.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER – Precision of SMALLDATETIME – A 1 Minute Precision</title>
		<link>http://blog.sqlauthority.com/2010/06/01/sql-server-precision-of-smalldatetime-a-1-minute-precision/</link>
		<comments>http://blog.sqlauthority.com/2010/06/01/sql-server-precision-of-smalldatetime-a-1-minute-precision/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 01:30:28 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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=9117</guid>
		<description><![CDATA[I am myself surprised that I am writing this post today. I am going to present one of the very known facts of SQL Server SMALLDATETIME datatype. Even though this is a very well-known datatype, many a time, I have seen developers getting confused with precision of the SMALLDATETIME datatype. The precision of the datatype [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9117&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I am myself surprised that I am writing this post today. I am going to present one of the very known facts of SQL Server SMALLDATETIME datatype. Even though this is a very well-known datatype, many a time, I have seen developers getting confused with precision of the SMALLDATETIME datatype.</p>
<p style="text-align:justify;">The precision of the datatype SMALLDATETIME is 1 minute. It discards the seconds by rounding up or rounding down any seconds greater than zero. Let us see the following example</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@varSDate </span><span style="color:blue;">AS </span><span style="color:black;">SMALLDATETIME<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@varSDate </span><span style="color:blue;">= </span><span style="color:red;">'1900-01-01 12:12:01'<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@varSDate </span><span style="color:black;">C_SDT<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@varSDate </span><span style="color:blue;">= </span><span style="color:red;">'1900-01-01 12:12:29'<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@varSDate </span><span style="color:black;">C_SDT<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@varSDate </span><span style="color:blue;">= </span><span style="color:red;">'1900-01-01 12:12:30'<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@varSDate </span><span style="color:black;">C_SDT<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@varSDate </span><span style="color:blue;">= </span><span style="color:red;">'1900-01-01 12:12:59'<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@varSDate </span><span style="color:black;">C_SDT<br />
</span></code></p>
<p style="text-align:justify;">Following is the result of the above script and note that any value between 0 (zero) and 59 is converted up or down.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/smalldatetimep.jpg" alt="" width="500" height="341" /></p>
<p style="text-align:justify;">The part that confuses the developers is the value of the seconds in the display. I think if it is not maintained or recorded, it should not be displayed 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/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-datetime/'>SQL DateTime</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/9117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9117/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9117&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/06/01/sql-server-precision-of-smalldatetime-a-1-minute-precision/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/smalldatetimep.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; DATE and TIME in SQL Server 2008</title>
		<link>http://blog.sqlauthority.com/2010/05/27/sql-server-date-and-time-in-sql-server-2008/</link>
		<comments>http://blog.sqlauthority.com/2010/05/27/sql-server-date-and-time-in-sql-server-2008/#comments</comments>
		<pubDate>Thu, 27 May 2010 01:30:38 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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=9112</guid>
		<description><![CDATA[I was thinking about DATE and TIME datatypes in SQL Server 2008. I earlier wrote about the about best practices of the same. Recently I had written one of the script written for SQL Server 2008 had to run on SQL Server 2005 (don&#8217;t ask me why!), I had to convert the DATE and TIME [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9112&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I was thinking about DATE and TIME <a href="http://blog.sqlauthority.com/2007/12/23/sql-server-2008-new-datatypes-date-and-time/" target="_blank">datatypes in SQL Server 2008</a>. I earlier wrote about the about <a href="http://blog.sqlauthority.com/2008/10/18/sql-server-retrieve-select-only-date-part-from-datetime-best-practice-part-2/" target="_blank">best practices</a> of the same. Recently I had written one of the script written for SQL Server 2008 had to run on SQL Server 2005 (don&#8217;t ask me why!), I had to convert the DATE and TIME datatypes to DATETIME. Let me run quick demo for the same.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@varDate </span><span style="color:blue;">AS </span><span style="color:black;">DATE<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@varTime </span><span style="color:blue;">AS </span><span style="color:black;">TIME<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@varDate </span><span style="color:blue;">= </span><span style="color:red;">'10/10/2010'<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@varTime </span><span style="color:blue;">= </span><span style="color:red;">'12:12:12'<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:#434343;">@varDate </span><span style="color:blue;">AS </span><span style="color:black;">DATETIME</span><span style="color:gray;">) </span><span style="color:black;">C_Date<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:#434343;">@varTime </span><span style="color:blue;">AS </span><span style="color:black;">DATETIME</span><span style="color:gray;">) </span><span style="color:black;">C_Time<br />
</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/convertdatetime.jpg" alt="" width="500" height="278" /></p>
<p style="text-align:justify;">As seen in example when DATE is converted to DATETIME it adds the of midnight. When TIME is converted to DATETIME it adds the date of 1900 and it is something one wants to consider if you are going to run script from SQL Server 2008 to earlier version with CONVERT.</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/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-datetime/'>SQL DateTime</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/9112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9112&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/05/27/sql-server-date-and-time-in-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>11</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/convertdatetime.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Get Date of All Weekdays or Weekends of the Year</title>
		<link>http://blog.sqlauthority.com/2009/12/29/sql-server-get-date-of-all-weekdays-or-weekends-of-the-year/</link>
		<comments>http://blog.sqlauthority.com/2009/12/29/sql-server-get-date-of-all-weekdays-or-weekends-of-the-year/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 01:30:56 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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=7648</guid>
		<description><![CDATA[Today&#8217;s article is created based on wonderful contribution from Tejas Shah. Tejas is very prominent SQL Expert and .NET wizard. He has answered the query of a reader on this blog who raised the following question: how to generate the date for all the Sundays in the upcoming year. Tejas replied here with a script. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=7648&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Today&#8217;s article is created based on wonderful contribution from <a href="http://www.sqlyoga.com/" target="_blank">Tejas Shah</a>. Tejas is very prominent SQL Expert and .NET wizard. He has answered the query of a reader on this blog who raised the following question: how to generate the date for all the Sundays in the upcoming year. Tejas replied <a href="http://blog.sqlauthority.com/2007/05/13/sql-server-query-to-find-first-and-last-day-of-current-month/#comment-58694" target="_blank"><strong>here </strong></a>with a script.</p>
<p style="text-align:justify;">What I really liked about the script is that it is very easy to understand, and also it can be customized very quickly.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Year </span><span style="color:blue;">AS INT</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@FirstDateOfYear </span><span style="color:black;">DATETIME</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@LastDateOfYear </span><span style="color:black;">DATETIME<br />
</span><span style="color:green;">-- You can change @year to any year you desire<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@year </span><span style="color:blue;">= </span><span style="color:black;">2010<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@FirstDateOfYear </span><span style="color:blue;">= </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">yyyy</span><span style="color:gray;">, </span><span style="color:#434343;">@Year </span><span style="color:gray;">- </span><span style="color:black;">1900</span><span style="color:gray;">, </span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@LastDateOfYear </span><span style="color:blue;">= </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">yyyy</span><span style="color:gray;">, </span><span style="color:#434343;">@Year </span><span style="color:gray;">- </span><span style="color:black;">1900 </span><span style="color:gray;">+ </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:green;">-- Creating Query to Prepare Year Data<br />
</span><span style="color:gray;">;</span><span style="color:blue;">WITH </span><span style="color:black;">cte </span><span style="color:blue;">AS </span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">DayID</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@FirstDateOfYear </span><span style="color:blue;">AS </span><span style="color:black;">FromDate</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:#434343;">@FirstDateOfYear</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">Dayname<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">cte.DayID </span><span style="color:gray;">+ </span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">DayID</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">, </span><span style="color:black;">1 </span><span style="color:gray;">,</span><span style="color:black;">cte.FromDate</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:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">, </span><span style="color:black;">1 </span><span style="color:gray;">,</span><span style="color:black;">cte.FromDate</span><span style="color:gray;">)) </span><span style="color:blue;">AS </span><span style="color:black;">Dayname<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">cte<br />
</span><span style="color:blue;">WHERE </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">,</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:black;">cte.FromDate</span><span style="color:gray;">) &lt; </span><span style="color:#434343;">@LastDateOfYear<br />
</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">FromDate </span><span style="color:blue;">AS </span><span style="color:black;">Date</span><span style="color:gray;">, </span><span style="color:black;">Dayname<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">CTE<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">DayName </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:red;">'Saturday'</span><span style="color:gray;">,</span><span style="color:red;">'Sunday'</span><span style="color:gray;">)<br />
</span><span style="color:green;">/*<br />
WHERE DayName IN ('Saturday,Sunday') -- For Weekend<br />
WHERE DayName NOT IN ('Saturday','Sunday') -- For Weekday<br />
WHERE DayName LIKE 'Monday' -- For Monday<br />
WHERE DayName LIKE 'Sunday' -- For Sunday<br />
*/<br />
</span><span style="color:blue;">OPTION </span><span style="color:gray;">(</span><span style="color:black;">MaxRecursion 370</span><span style="color:gray;">)</span></code></p>
<p style="text-align:justify;">The result will be dates along with days in next column as expected.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/calweekend.jpg" alt="" width="430" height="281" /></p>
<p style="text-align:justify;">You are welcome to contribute any script, which you think can be helpful to others.</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 />Posted in Pinal Dave, Readers Contribution, SQL, SQL Authority, SQL DateTime, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7648/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7648/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/7648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/7648/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/7648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/7648/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7648/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7648/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7648/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=7648&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/12/29/sql-server-get-date-of-all-weekdays-or-weekends-of-the-year/feed/</wfw:commentRss>
		<slash:comments>21</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/calweekend.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Get Time in Hour:Minute Format from a Datetime &#8211; Get Date Part Only from Datetime</title>
		<link>http://blog.sqlauthority.com/2009/08/06/sql-server-get-time-in-hourminute-format-from-a-datetime-get-date-part-only-from-datetime/</link>
		<comments>http://blog.sqlauthority.com/2009/08/06/sql-server-get-time-in-hourminute-format-from-a-datetime-get-date-part-only-from-datetime/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 01:30:17 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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=5940</guid>
		<description><![CDATA[I have seen scores of expert developers getting perplexed with SQL Server in finding time only from datetime datatype. Let us have a quick glance look at the solution. SQL Server 2000/2005 SELECT CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond, CONVERT(VARCHAR(8),GETDATE(),101) AS DateOnly GO SQL Server 2008 SELECT CONVERT(TIME,GETDATE()) AS HourMinuteSecond, CONVERT(DATE,GETDATE(),101) AS DateOnly GO I hope the above [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=5940&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I have seen scores of expert developers getting perplexed with SQL Server in finding time only from datetime datatype. Let us have a quick glance look at the solution.</p>
<p style="text-align:justify;"><strong>SQL Server 2000/2005</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT<br />
</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">8</span><span style="color:gray;">),</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(),</span><span style="color:black;">108</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">HourMinuteSecond</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">8</span><span style="color:gray;">),</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(),</span><span style="color:black;">101</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">DateOnly<br />
GO</span></code></p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/datetime2005.jpg" alt="" width="489" height="188" /><br />
</span>
</p>
<p style="text-align:justify;"><strong>SQL Server 2008</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT<br />
</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:black;">TIME</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:black;">HourMinuteSecond</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:black;">DATE</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(),</span><span style="color:black;">101</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">DateOnly<br />
GO</span></code></p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/datetime2008.jpg" alt="" width="413" height="184" /><br />
</span>
</p>
<p style="text-align:justify;">I hope the above solution is clear to you all.</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 />Posted in Pinal Dave, SQL, SQL Authority, SQL DateTime, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/5940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/5940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/5940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/5940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5940/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=5940&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/08/06/sql-server-get-time-in-hourminute-format-from-a-datetime-get-date-part-only-from-datetime/feed/</wfw:commentRss>
		<slash:comments>113</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/datetime2005.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/datetime2008.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Find Last Date Time Updated for Any Table</title>
		<link>http://blog.sqlauthority.com/2009/05/09/sql-server-find-last-date-time-updated-for-any-table/</link>
		<comments>http://blog.sqlauthority.com/2009/05/09/sql-server-find-last-date-time-updated-for-any-table/#comments</comments>
		<pubDate>Sat, 09 May 2009 01:30:17 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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=4940</guid>
		<description><![CDATA[I just received an email from one of my regular readers who is curious to know if there is any way to find out when a table is recently updated. I was ready with my answer! I promptly suggested him that if a table contains UpdatedDate or ModifiedDate date column with default together with value [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=4940&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I just received an email from one of my regular readers who is curious to know if there is any way to find out when a table is recently updated. I was ready with my answer! I promptly suggested him that if a table contains UpdatedDate or ModifiedDate date column with default together with value GETDATE(), he should make use of it. On close observation the table is not required to keep history when any row is inserted. However, the sole prerequisite is to be aware of when any table has been updated. That’s it!</p>
<p style="text-align:justify;">If a user wants to finds out when was the last table updated he can query dynamic management view (dmv) &#8211; sys.dm_db_index_usage_stats and easily figure out when was the table updated last. Let us comprehend this example by creating a table and updating it. We can use dmv to determine when it was updated last.</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;">CREATE TABLE </span><span style="color:black;">Test<br />
</span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">,<br />
</span><span style="color:black;">COL </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">))<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">Test<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:red;">'First'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:red;">'Second'<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">Now we have created a table and populated it with data. Next, we will run the following query to find out when it was last updated.</p>
<p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">DatabaseName</span><span style="color:gray;">, </span><span style="color:black;">last_user_update</span><span style="color:gray;">,*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_db_index_usage_stats<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">database_id </span><span style="color:blue;">= </span><span style="color:magenta;">DB_ID</span><span style="color:gray;">( </span><span style="color:red;">'AdventureWorks'</span><span style="color:gray;">)<br />
AND </span><span style="color:magenta;">OBJECT_ID</span><span style="color:blue;">=</span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'test'</span><span style="color:gray;">)</span></code></p>
<p style="text-align:justify;">Running query provides accurate details of when was  the table last updated. If WHERE condition is entirely removed it will provide details of the entire database.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/lastupdated.jpg" alt="" width="500" height="502" /></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 />Posted in Pinal Dave, Readers Question, SQL, SQL Authority, SQL DateTime, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/4940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/4940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/4940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/4940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/4940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/4940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/4940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/4940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/4940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/4940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/4940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/4940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/4940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/4940/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=4940&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/05/09/sql-server-find-last-date-time-updated-for-any-table/feed/</wfw:commentRss>
		<slash:comments>99</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/lastupdated.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Time Delay While Running T-SQL Query &#8211; WAITFOR Introduction</title>
		<link>http://blog.sqlauthority.com/2009/01/03/sql-server-time-delay-while-running-t-sql-query-waitfor-introduction/</link>
		<comments>http://blog.sqlauthority.com/2009/01/03/sql-server-time-delay-while-running-t-sql-query-waitfor-introduction/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 01:30:08 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL DateTime]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[Best Practices]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=1947</guid>
		<description><![CDATA[Today we will look at one very small but interesting feature of SQL Server. Please note that this is not much known feature of SQL Server. In SQL Server sometime there are requirement when T-SQL script has to wait for some time before executing next statement. It is quite common that developers depends on application [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1947&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Today we will look at one very small but interesting feature of SQL Server. Please note that this is not much known feature of SQL Server. In SQL Server sometime there are requirement when T-SQL script has to wait for some time before executing next statement. It is quite common that developers depends on application to take over this delay issue. However, SQL Server itself has very strong time management function of WAITFOR. Let us see two usage of WAITFOR clause.</p>
<p style="text-align:justify;">Official explanation of WAITFOR clause from Book Online is &#8220;Blocks the execution of a batch, stored procedure, or transaction until a specified time or time interval is reached, or a specified statement modifies or returns at least one row.&#8221;</p>
<p style="text-align:justify;"><strong>Option 1 : Waitfor Delay in executing T-SQL</strong><br />
T-SQL runs after particular delay is completed.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:black;">CurrentTime<br />
</span><span style="color:blue;">WAITFOR </span><span style="color:black;">DELAY </span><span style="color:red;">'00:00:05' </span><span style="color:green;">---- 5 Second Delay<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:black;">CurrentTime</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/waitfor1.jpg" alt="" width="400" height="250" /></p>
<p style="text-align:justify;"><strong>Option 2 : Waitfor Time in executing T-SQL</strong><br />
T-SQL runs after particular time has arrived.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@MyDateTime </span><span style="color:black;">DATETIME<br />
</span><span style="color:green;">/* Add 5 seconds to current time so<br />
system waits for 5 seconds*/<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@MyDateTime </span><span style="color:blue;">= </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">s</span><span style="color:gray;">,</span><span style="color:black;">5</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">())<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:black;">CurrentTime<br />
</span><span style="color:blue;">WAITFOR </span><span style="color:black;">TIME </span><span style="color:#434343;">@MyDateTime<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:black;">CurrentTime </span></code>
</p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/waitfor2.jpg" alt="" width="400" height="300" /></span></p>
<p style="text-align:justify;"><span style="color:black;">Reference : <strong>Pinal Dave (<a href="http://blog.SQLAuthority.com" target="_blank">http://blog.SQLAuthority.com</a>)</strong><br />
</span></p>
<br />Posted in Best Practices, Pinal Dave, SQL, SQL Authority, SQL DateTime, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQL Utility, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1947/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1947/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/1947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/1947/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/1947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/1947/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1947/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1947/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1947/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1947&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/01/03/sql-server-time-delay-while-running-t-sql-query-waitfor-introduction/feed/</wfw:commentRss>
		<slash:comments>11</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/waitfor1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/waitfor2.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Retrieve &#8211; Select Only Date Part From DateTime &#8211; Best Practice &#8211; Part 2</title>
		<link>http://blog.sqlauthority.com/2008/10/18/sql-server-retrieve-select-only-date-part-from-datetime-best-practice-part-2/</link>
		<comments>http://blog.sqlauthority.com/2008/10/18/sql-server-retrieve-select-only-date-part-from-datetime-best-practice-part-2/#comments</comments>
		<pubDate>Sat, 18 Oct 2008 01:30:28 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Performance]]></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://sqlauthority.wordpress.com/?p=1383</guid>
		<description><![CDATA[A year ago I wrote post about SQL SERVER &#8211; Retrieve &#8211; Select Only Date Part From DateTime &#8211; Best Practice where I have discussed two different methods of getting datepart from datetime. Method 1: SELECT DATEADD(D, 0, DATEDIFF(D, 0, GETDATE())) Method 2: SELECT CONVERT(VARCHAR(10),GETDATE(),111) I have summarized my post suggesting that either method works [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1383&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">A year ago I wrote post about <a href="http://blog.sqlauthority.com/2007/06/10/sql-server-retrieve-select-only-date-part-from-datetime-best-practice/" target="_blank">SQL SERVER &#8211; Retrieve &#8211; Select Only Date Part From DateTime &#8211; Best Practice</a> where I have discussed two different methods of getting datepart from datetime.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/datetimecs2.gif" alt="" width="382" height="395" /></p>
<p style="text-align:justify;">Method 1:<br />
<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:black;">D</span><span style="color:gray;">, </span><span style="color:black;">0</span><span style="color:gray;">, </span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">D</span><span style="color:gray;">, </span><span style="color:black;">0</span><span style="color:gray;">, </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()))</span></code></p>
<p style="text-align:justify;">Method 2:<br />
<code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">),</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(),</span><span style="color:black;">111</span><span style="color:gray;">)</span></code>
</p>
<p style="text-align:justify;">I have summarized my post suggesting that either method works fine and I prefer to use Method 2. However, with additional tests and looking at SQL Server internals very carefully, I want to suggest that Method 1 is better in terms of performance. While running on GETDATE() both of the above functions are equally fast and efficient. However, when above query is ran on very large data table performance different is visible.</p>
<p style="text-align:justify;">For testing purpose I have ran few queries where I am getting same result.</p>
<p style="text-align:justify;">Run Following query 10 times with client statistics on ( Shift+Alter+S) in SSMS.</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:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">, </span><span style="color:black;">0</span><span style="color:gray;">, </span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">, </span><span style="color:black;">0</span><span style="color:gray;">, </span><span style="color:black;">StartDate</span><span style="color:gray;">))<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.WorkOrder<br />
GO</span></code>
</p>
<p style="text-align:justify;">Now run another query 10 times with client statistics on.</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:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">),</span><span style="color:black;">StartDate</span><span style="color:gray;">,</span><span style="color:black;">111</span><span style="color:gray;">)<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.WorkOrder<br />
GO</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/datetimecs1.gif" alt="" width="382" height="395" /></p>
<p style="text-align:justify;">While looking at the average of statistics in it is clear that first query with DATEADD is much more efficient.</p>
<p style="text-align:justify;">Let us compare both of the above script together and see the client statistics.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/datetimecs.gif" alt="" width="549" height="580" /></p>
<p style="text-align:justify;">While comparing Numbers of Bytes sent across client and server as well client processing time, and total execution time in all case DATEADD is better. I suggest all my readers to use DATEADD when only DATEPART is required.</p>
<p style="text-align:justify;">If you are using SQL Server 2008 it has new DATE datatype which does not have time part. I suggest to use DATE datatype or TIME datatype for users of SQL Server 2008. Read more <a href="http://blog.sqlauthority.com/2007/12/23/sql-server-2008-new-datatypes-date-and-time/" target="_blank">SQL SERVER &#8211; 2008 &#8211; New DataTypes DATE and TIME</a><a href="http://blog.sqlauthority.com/2007/12/23/sql-server-2008-new-datatypes-date-and-time/" target="_blank">.</a></p>
<p style="text-align:justify;">If you want your date in any other format refer the UDF <a href="http://blog.sqlauthority.com/2008/08/14/sql-server-get-date-time-in-any-format-udf-user-defined-functions/" target="_blank">SQL SERVER &#8211; Get Date Time in Any Format &#8211; UDF &#8211; User Defined Functions</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></p>
<br />Posted in Best Practices, Pinal Dave, SQL, SQL Authority, SQL DateTime, SQL Function, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/1383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/1383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/1383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/1383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1383/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1383&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/10/18/sql-server-retrieve-select-only-date-part-from-datetime-best-practice-part-2/feed/</wfw:commentRss>
		<slash:comments>14</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/datetimecs2.gif" medium="image" />

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

		<media:content url="http://www.pinaldave.com/bimg/datetimecs.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers Complete List Download</title>
		<link>http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/</link>
		<comments>http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 01:30:39 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Cursor]]></category>
		<category><![CDATA[SQL Data Storage]]></category>
		<category><![CDATA[SQL DateTime]]></category>
		<category><![CDATA[SQL Documentation]]></category>
		<category><![CDATA[SQL Download]]></category>
		<category><![CDATA[SQL Error Messages]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Security]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBCC]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Trigger]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[SQL XML]]></category>
		<category><![CDATA[SQLAuthority]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1134</guid>
		<description><![CDATA[SQL Server Interview Questions and Answers Print Book Available (207 Pages) &#124; Sample Chapters Download SQL Server 2008 Interview Questions and Answers Complete List UPDATE: This article series has been updated with new interview questions and answers series. Interview is very important event for any person. A good interview leads to good career if candidate [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1134&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<table class="sample">
<tr>
<td>SQL Server Interview Questions and Answers</td>
</tr>
<tr>
<td><a href="http://bit.ly/sqlinterviewbook" target="_blank" class="sample1">Print Book Available (207 Pages)</a> | <a href="http://www.pinaldave.com/sql-downloads/pdf-download/sql-server-2008-interview-questions-and-answers-download/" target="_blank" class="sample1">Sample Chapters</a></td>
</tr>
</table>
<h2 style="text-align:justify;"><a href="http://blog.sqlauthority.com/sql-server-interview-questions-and-answers/"><strong>Download SQL Server 2008 Interview Questions and Answers Complete List</strong></a></h2>
<p style="text-align:justify;"><span style="color:#ff0000;"><strong>UPDATE:</strong></span> <a href="http://blog.sqlauthority.com/sql-server-interview-questions-and-answers/">This article series has been updated with new interview questions and answers series.</a></p>
<p style="text-align:justify;">Interview is very important event for any person. A good interview leads to good career if candidate is willing to learn. I always enjoy interview questions and answers series. This is my very humble attempt to write SQL Server 2008 interview questions and answers. SQL Server is very large subject and not everything is usually asked in interview. In interview what matters the most is <strong>conceptual knowledge</strong> and <strong>learning attitude</strong>.</p>
<p style="text-align:justify;">I have listed all the series in this post so that it can be easily downloaded and used. All the questions are collected and listed in one PDF which is here to download. If you have any question or if you want to add to any of the question please send me mail or write a comment.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/12/sql-server-2008-interview-questions-and-answers-part-1/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 1</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/13/sql-server-2008-interview-questions-and-answers-part-2/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 2</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/14/sql-server-2008-interview-questions-and-answers-part-3/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 3</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/15/sql-server-2008-interview-questions-and-answers-part-4/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 4</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/16/sql-server-2008-interview-questions-and-answers-part-5/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 5</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/17/sql-server-2008-interview-questions-and-answers-part-6/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 6</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/18/sql-server-2008-interview-questions-and-answers-part-7/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 7</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/19/sql-server-2008-interview-questions-and-answers-part-8/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 8</strong></a></p>
<h2 style="text-align:justify;"><a href="http://www.pinaldave.com/sql-downloads/pdf-download/sql-server-2008-interview-questions-and-answers-download/"><strong>Download SQL Server 2008 Interview Questions and Answers Complete List</strong></a></h2>
<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 />Posted in Database, Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Cursor, SQL Data Storage, SQL DateTime, SQL Documentation, SQL Download, SQL Error Messages, SQL Function, SQL Index, SQL Interview Questions and Answers, SQL Joins, SQL Performance, SQL Query, SQL Scripts, SQL Security, SQL Server, SQL Server DBCC, SQL Stored Procedure, SQL Tips and Tricks, SQL Trigger, SQL Utility, SQL XML, SQLAuthority, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1134/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1134&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/feed/</wfw:commentRss>
		<slash:comments>141</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; 2008 &#8211; Interview Questions and Answers &#8211; Part 6</title>
		<link>http://blog.sqlauthority.com/2008/09/17/sql-server-2008-interview-questions-and-answers-part-6/</link>
		<comments>http://blog.sqlauthority.com/2008/09/17/sql-server-2008-interview-questions-and-answers-part-6/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 01:30:05 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></category>
		<category><![CDATA[SQL DateTime]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[SQLAuthority]]></category>
		<category><![CDATA[SQL Data Storage]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1008</guid>
		<description><![CDATA[SQL Server Interview Questions and Answers Print Book Available (207 Pages) &#124; Sample Chapters SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers Complete List Download 3) Questions of SQL SERVER 2008 What are the basic functions for master, msdb, model, tempdb and resource databases? The master database holds information for all databases located on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1008&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<table class="sample">
<tr>
<td>SQL Server Interview Questions and Answers</td>
</tr>
<tr>
<td><a href="http://bit.ly/sqlinterviewbook" target="_blank" class="sample1">Print Book Available (207 Pages)</a> | <a href="http://www.pinaldave.com/sql-downloads/pdf-download/sql-server-2008-interview-questions-and-answers-download/" target="_blank" class="sample1">Sample Chapters</a></td>
</tr>
</table>
<h2 style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/" target="_blank">SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers Complete List Download</a></strong></h2>
<p style="text-align:justify;"><strong>3) </strong><strong>Questions of SQL SERVER 2008</strong></p>
<p style="text-align:justify;"><strong>What are the basic functions for master, msdb, model, tempdb and resource databases?</strong></p>
<p style="text-align:justify;"><em>The <strong>master</strong> database </em>holds information for all databases located on the SQL Server instance and is theglue that holds the engine together. Because SQL Server cannot start without a functioning masterdatabase, you must administer this database with care.</p>
<p style="text-align:justify;"><em>The <strong>msdb</strong> database </em>stores information regarding database backups, SQL Agent information, DTS packages, SQL Server jobs, and some replication information such as for log shipping.</p>
<p style="text-align:justify;"><em>The <strong>tempdb</strong> </em>holds temporary objects such as global and local temporary tables and stored procedures.</p>
<p style="text-align:justify;"><em>The <strong>model</strong></em> is essentially a template database used in the creation of any new user database created in the instance.</p>
<p style="text-align:justify;"><em>The <strong>resoure</strong> Database </em>is a read-only database that contains all the system objects that are included with SQL Server. SQL Server system objects, such as sys.objects, are physically persisted in the Resource database, but they logically appear in the sys schema of every database. The Resource database does not contain user data or user metadata.</p>
<p style="text-align:justify;"><strong>What is Service Broker?</strong></p>
<p style="text-align:justify;">Service Broker is a message-queuing technology in SQL Server that allows developers to integrate SQL Server fully into distributed applications. Service Broker is feature which provides facility to SQL Server to send an asynchronous, transactional message. it allows a database to send a message to another database without waiting for the response, so the application will continue to function if the remote database is temporarily unavailable. (<a href="http://blog.sqlauthority.com/2008/07/18/sql-server-introduction-to-service-broker/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>Where SQL server user names and passwords are stored in SQL server?</strong></p>
<p style="text-align:justify;">They get stored in System Catalog Views sys.server_principals and sys.sql_logins.</p>
<p style="text-align:justify;"><strong>What is Policy Management?</strong></p>
<p style="text-align:justify;">Policy Management in SQL SERVER 2008 allows you to define and enforce policies for configuring and managing SQL Server across the enterprise. Policy-Based Management is configured in SQL Server Management Studio (SSMS).  Navigate to the Object Explorer and expand the Management node and the Policy Management node; you will see the Policies, Conditions, and Facets nodes. (<a href="http://blog.sqlauthority.com/2008/06/13/sql-server-2008-introduction-to-policy-management-enforcing-rules-on-sql-server/" target="_blank">Read More Here</a>)<strong></strong></p>
<p style="text-align:justify;"><strong>What is Replication and Database Mirroring?</strong></p>
<p style="text-align:justify;">Database mirroring can be used with replication to provide availability for the publication database. Database mirroring involves two copies of a single database that typically reside on different computers. At any given time, only one copy of the database is currently available to clients which are known as the principal database. Updates made by clients to the principal database are applied on the other copy of the database, known as the mirror database. Mirroring involves applying the transaction log from every insertion, update, or deletion made on the principal database onto the mirror database.</p>
<p style="text-align:justify;"><strong>What are Sparse Columns?</strong></p>
<p style="text-align:justify;">A sparse column<em> </em>is another tool used to reduce the amount of physical storage used in a database. They are the ordinary columns that have an optimized storage for null values. Sparse columns reduce the space requirements for null values at the cost of more overhead to retrieve nonnull values. (<a href="http://blog.sqlauthority.com/2008/07/10/sql-server-2008-introduction-to-sparse-columns/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>What does TOP Operator Do?</strong></p>
<p style="text-align:justify;">The TOP operator is used to specify the number of rows to be returned by a query. The TOP operator has new addition in SQL SERVER 2008 that it accepts variables as well as literal values and can be used with INSERT, UPDATE, and DELETES statements.</p>
<p style="text-align:justify;"><strong>What is CTE?</strong></p>
<p style="text-align:justify;">CTE is an abbreviation Common Table Expression. A Common Table Expression (CTE) is an expression that can be thought of as a temporary result set which is defined within the execution of a single SQL statement<strong>. </strong>A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query. (<a href="http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>What is MERGE Statement?</strong><strong><br />
</strong>MERGE is a new feature that provides an efficient way to perform multiple DML operations. In previous versions of SQL Server, we had to write separate statements to INSERT, UPDATE, or DELETE data based on certain conditions, but now, using MERGE statement we can include the logic of such data modifications in one statement that even checks when the data is matched then just update it and when unmatched then insert it. One of the most important advantages of MERGE statement is all the data is read and processed only once.  (<a href="http://blog.sqlauthority.com/2008/08/28/sql-server-2008-introduction-to-merge-statement-one-statement-for-insert-update-delete/" target="_blank">Read More Here</a>)
</p>
<p style="text-align:justify;"><strong>What is Filtered Index?</strong></p>
<p style="text-align:justify;">Filtered Index is used to index a portion of rows in a table that means it applies filter on INDEX which improves query performance, reduce index maintenance costs, and reduce index storage costs compared with full-table indexes. When we see an Index created with some where clause then that is actually a FILTERED INDEX.</p>
<p style="text-align:justify;"><strong>Which are new data types introduced in SQL SERVER 2008?</strong></p>
<p style="text-align:justify;"><strong><em>The GEOMETRY Type: </em></strong>The GEOMETRY data type is a system .NET common language runtime (CLR) data type in SQL Server. This type represents data in a two-dimensional Euclidean coordinate system.</p>
<p style="text-align:justify;"><strong><em>The GEOGRAPHY Type:</em></strong> The GEOGRAPHY datatype&#8217;s functions are the same as with GEOMETRY. The difference between the two is that when you specify GEOGRAPHY, you are usually specifying points in terms of latitude and longitude.</p>
<p style="text-align:justify;"><strong><em>New Date and Time Datatypes:</em></strong> SQL Server 2008 introduces four new datatypes related to date and time: DATE, TIME, DATETIMEOFFSET, and DATETIME2.</p>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong><em>DATE</em></strong><em>:</em> The new DATE type just stores the date itself. It is based on the Gregorian calendar and handles years from 1 to 9999.</li>
<li> <strong><em>TIME</em></strong><em>:</em> The new TIME (<em>n</em>) type stores time with a range of 00:00:00.0000000 through 23:59:59.9999999. The precision is allowed with this type. TIME supports seconds down to 100 nanoseconds. The <em>n </em>in TIME (<em>n</em>) defines this level of fractional second precision, from 0 to 7 digits of precision.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong><em>The DATETIMEOFFSET Type</em></strong>: DATETIMEOFFSET (<em>n</em>) is the time-zone-aware version of a datetime datatype. The name will appear less odd when you consider what it really is: a date + a time + a time-zone offset. The offset is based on how far behind or ahead you are from Coordinated Universal Time (UTC) time.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong><em>The DATETIME2 Type</em></strong>: It is an extension of the datetime type in earlier versions of SQL Server. This new datatype has a date range covering dates from January 1 of year 1 through December 31 of year 9999. This is a definite improvement over the 1753 lower boundary of the datetime datatype. DATETIME2 not only includes the larger date range, but also has a timestamp and the same fractional precision that TIME type provides</li>
</ul>
<p style="text-align:justify;"><strong>What are the Advantages of using CTE?</strong></p>
<ul class="unIndentedList" style="text-align:justify;">
<li> Using CTE improves the readability and makes maintenance of complex queries easy.</li>
<li> The query can be divided into separate, simple, logical building blocks which can be then used to build more complex CTEs until final result set is generated.</li>
<li> CTE can be defined in functions, stored procedures, triggers or even views.</li>
<li> After a CTE is defined, it can be used as a Table or a View and can SELECT, INSERT, UPDATE or DELETE Data.</li>
</ul>
<p style="text-align:justify;">© Copyright 2000-2009<a title="Pinal Dave" href="http://www.pinaldave.com/" target="_blank"> Pinal Dave.</a> All Rights Reserved. <a href="http://blog.sqlauthority.com/" target="_blank">SQLAuthority.com</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></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/1008/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/1008/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/1008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/1008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/1008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/1008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1008/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=1008&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/17/sql-server-2008-interview-questions-and-answers-part-6/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>
	</item>
		<item>
		<title>SQL SERVER &#8211; Few Useful DateTime Functions to Find Specific Dates</title>
		<link>http://blog.sqlauthority.com/2008/08/29/sql-server-few-useful-datetime-functions-to-find-specific-dates/</link>
		<comments>http://blog.sqlauthority.com/2008/08/29/sql-server-few-useful-datetime-functions-to-find-specific-dates/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 01:30:00 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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://sqlauthority.wordpress.com/?p=916</guid>
		<description><![CDATA[Recently I have recieved email from Vivek Jamwal, which contains many useful SQL Server Date functions. ----Today SELECT GETDATE() 'Today' ----Yesterday SELECT DATEADD(d,-1,GETDATE()) 'Yesterday' ----First Day of Current Week SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0) 'First Day of Current Week' ----Last Day of Current Week SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),6) 'Last Day of Current Week' ----First Day of Last Week SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0) 'First Day of Last Week' ----Last Day of Last Week SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),6) 'Last Day of Last Week' ----First Day of Current Month SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0) 'First Day of Current Month' ----Last Day of Current Month SELECT DATEADD(ms,- 3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0))) 'Last Day of Current Month' ----First Day of Last Month SELECT DATEADD(mm,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)) 'First Day of Last Month' ----Last Day of Last Month SELECT [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=916&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Recently I have recieved email from <span class="email"><strong>Vivek Jamwal</strong>, which contains many useful SQL Server Date functions. </span></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">----Today<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:red;">'Today'<br />
</span><span style="color:green;">----Yesterday<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">,-</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()) </span><span style="color:red;">'Yesterday'<br />
</span><span style="color:green;">----First Day of Current Week<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">wk</span><span style="color:gray;">,</span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">wk</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()),</span><span style="color:black;">0</span><span style="color:gray;">) </span><span style="color:red;">'First Day of Current Week'<br />
</span><span style="color:green;">----Last Day of Current Week<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">wk</span><span style="color:gray;">,</span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">wk</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()),</span><span style="color:black;">6</span><span style="color:gray;">) </span><span style="color:red;">'Last Day of Current Week'<br />
</span><span style="color:green;">----First Day of Last Week<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">wk</span><span style="color:gray;">,</span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">wk</span><span style="color:gray;">,</span><span style="color:black;">7</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()),</span><span style="color:black;">0</span><span style="color:gray;">) </span><span style="color:red;">'First Day of Last Week'<br />
</span><span style="color:green;">----Last Day of Last Week<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">wk</span><span style="color:gray;">,</span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">wk</span><span style="color:gray;">,</span><span style="color:black;">7</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()),</span><span style="color:black;">6</span><span style="color:gray;">) </span><span style="color:red;">'Last Day of Last Week'<br />
</span><span style="color:green;">----First Day of Current Month<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">mm</span><span style="color:gray;">,</span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">mm</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()),</span><span style="color:black;">0</span><span style="color:gray;">) </span><span style="color:red;">'First Day of Current Month'<br />
</span><span style="color:green;">----Last Day of Current Month<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">ms</span><span style="color:gray;">,- </span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">mm</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">mm</span><span style="color:gray;">,</span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">mm</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">())+</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">))) </span><span style="color:red;">'Last Day of Current Month'<br />
</span><span style="color:green;">----First Day of Last Month<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">mm</span><span style="color:gray;">,-</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">mm</span><span style="color:gray;">,</span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">mm</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()),</span><span style="color:black;">0</span><span style="color:gray;">)) </span><span style="color:red;">'First Day of Last Month'<br />
</span><span style="color:green;">----Last Day of Last Month<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">ms</span><span style="color:gray;">,-</span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">mm</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">mm</span><span style="color:gray;">,</span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">mm</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()),</span><span style="color:black;">0</span><span style="color:gray;">))) </span><span style="color:red;">'Last Day of Last Month'<br />
</span><span style="color:green;">----First Day of Current Year<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">yy</span><span style="color:gray;">,</span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">yy</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()),</span><span style="color:black;">0</span><span style="color:gray;">) </span><span style="color:red;">'First Day of Current Year'<br />
</span><span style="color:green;">----Last Day of Current Year<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">ms</span><span style="color:gray;">,-</span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">yy</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">yy</span><span style="color:gray;">,</span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">yy</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">())+</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">))) </span><span style="color:red;">'Last Day of Current Year'<br />
</span><span style="color:green;">----First Day of Last Year<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">yy</span><span style="color:gray;">,-</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">yy</span><span style="color:gray;">,</span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">yy</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()),</span><span style="color:black;">0</span><span style="color:gray;">)) </span><span style="color:red;">'First Day of Last Year'<br />
</span><span style="color:green;">----Last Day of Last Year<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">ms</span><span style="color:gray;">,-</span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">yy</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">yy</span><span style="color:gray;">,</span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">yy</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()),</span><span style="color:black;">0</span><span style="color:gray;">))) </span><span style="color:red;">'Last Day of Last Year' </span></code>
</p>
<p style="text-align:justify;"><strong>ResultSet:</strong></p>
<p style="text-align:justify;">Today<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
2008-08-29 21:54:58.967</p>
<p style="text-align:justify;">Yesterday<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
2008-08-28 21:54:58.967</p>
<p style="text-align:justify;">First Day of Current Week<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
2008-08-25 00:00:00.000</p>
<p style="text-align:justify;">Last Day of Current Week<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
2008-08-31 00:00:00.000</p>
<p style="text-align:justify;">First Day of Last Week<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
2008-08-18 00:00:00.000</p>
<p style="text-align:justify;">Last Day of Last Week<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
2008-08-24 00:00:00.000</p>
<p style="text-align:justify;">First Day of Current Month<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
2008-08-01 00:00:00.000</p>
<p style="text-align:justify;">Last Day of Current Month<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
2008-08-31 23:59:59.997</p>
<p style="text-align:justify;">First Day of Last Month<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
2008-07-01 00:00:00.000</p>
<p style="text-align:justify;">Last Day of Last Month<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
2008-07-31 23:59:59.997</p>
<p style="text-align:justify;">First Day of Current Year<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
2008-01-01 00:00:00.000</p>
<p style="text-align:justify;">Last Day of Current Year<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
2008-12-31 23:59:59.997</p>
<p style="text-align:justify;">First Day of Last Year<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
2007-01-01 00:00:00.000</p>
<p style="text-align:justify;">Last Day of Last Year<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
2007-12-31 23:59:59.997
</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>, <span class="email">Vivek Jamwal</span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/916/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/916/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/916/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/916/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=916&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/08/29/sql-server-few-useful-datetime-functions-to-find-specific-dates/feed/</wfw:commentRss>
		<slash:comments>69</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; Get Date Time in Any Format &#8211; UDF &#8211; User Defined Functions</title>
		<link>http://blog.sqlauthority.com/2008/08/14/sql-server-get-date-time-in-any-format-udf-user-defined-functions/</link>
		<comments>http://blog.sqlauthority.com/2008/08/14/sql-server-get-date-time-in-any-format-udf-user-defined-functions/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 01:30:13 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=820</guid>
		<description><![CDATA[One of the reader Nanda of SQLAuthority.com has posted very detailed script of converting any date time in desired format. I suggest every reader of this blog to save this script in your permanent code bookmark and use it when you need it. Refer the function and get familiar yourself with different format this function [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=820&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">One of the reader <a href="http://blog.sqlauthority.com/2008/08/01/sql-server-2008-get-current-system-date-time/#comment-41412" target="_blank">Nanda </a>of SQLAuthority.com has posted very detailed script of converting any date time in desired format. I suggest every reader of this blog to save this script in your permanent code bookmark and use it when you need it.</p>
<p style="text-align:justify;">Refer the function and get familiar yourself with different format this function support. I have added few examples of how this function can be used at the end of the article. You can download whole code in ZIP format as well.</p>
<p style="text-align:justify;"><a href="http://www.pinaldave.com/bimg/anyformatudf.zip" target="_blank">Download Get Date Time in Any Format Script (ZIP)</a></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">[dbo].[ufsFormat]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:#434343;">@Date </span><span style="color:black;">datetime</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@fORMAT </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">80</span><span style="color:gray;">)<br />
)<br />
</span><span style="color:blue;">RETURNS NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">80</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
BEGIN<br />
DECLARE </span><span style="color:#434343;">@Dateformat </span><span style="color:blue;">INT<br />
DECLARE </span><span style="color:#434343;">@ReturnedDate </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">80</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@TwelveHourClock </span><span style="color:blue;">INT<br />
DECLARE </span><span style="color:#434343;">@Before </span><span style="color:blue;">INT<br />
DECLARE </span><span style="color:#434343;">@pos </span><span style="color:blue;">INT<br />
DECLARE </span><span style="color:#434343;">@Escape </span><span style="color:blue;">INT </span><br />
<span style="color:green;">-- (c) Pinal Dave http://www.SQLAuthority.com </span></code><br />
<code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:#434343;">@ReturnedDate</span><span style="color:blue;">=</span><span style="color:red;">'error! unrecognised format '</span><span style="color:gray;">+</span><span style="color:#434343;">@format<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@DateFormat</span><span style="color:blue;">=</span><span style="color:magenta;">CASE </span><span style="color:#434343;">@format<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'mmm dd yyyy hh:mm AM/PM' </span><span style="color:blue;">THEN </span><span style="color:black;">100<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'mm/dd/yy' </span><span style="color:blue;">THEN </span><span style="color:black;">1<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'mm/dd/yyyy' </span><span style="color:blue;">THEN </span><span style="color:black;">101<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'yy.mm.dd' </span><span style="color:blue;">THEN </span><span style="color:black;">2<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'dd/mm/yy' </span><span style="color:blue;">THEN </span><span style="color:black;">3<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'dd.mm.yy' </span><span style="color:blue;">THEN </span><span style="color:black;">4<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'dd-mm-yy' </span><span style="color:blue;">THEN </span><span style="color:black;">5<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'dd Mmm yy' </span><span style="color:blue;">THEN </span><span style="color:black;">6<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Mmm dd, yy' </span><span style="color:blue;">THEN </span><span style="color:black;">7<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'hh:mm:ss' </span><span style="color:blue;">THEN </span><span style="color:black;">8<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'yyyy.mm.dd' </span><span style="color:blue;">THEN </span><span style="color:black;">102<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'dd/mm/yyyy' </span><span style="color:blue;">THEN </span><span style="color:black;">103<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'dd.mm.yyyy' </span><span style="color:blue;">THEN </span><span style="color:black;">104<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'dd-mm-yyyy' </span><span style="color:blue;">THEN </span><span style="color:black;">105<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'dd Mmm yyyy' </span><span style="color:blue;">THEN </span><span style="color:black;">106<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Mmm dd, yyyy' </span><span style="color:blue;">THEN </span><span style="color:black;">107<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Mmm dd yyyy hh:mm:ss:ms AM/PM' </span><span style="color:blue;">THEN </span><span style="color:black;">9<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Mmm dd yyyy hh:mi:ss:mmm AM/PM' </span><span style="color:blue;">THEN </span><span style="color:black;">9<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Mmm dd yy hh:mm:ss:ms AM/PM' </span><span style="color:blue;">THEN </span><span style="color:black;">109<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'mm-dd-yy' </span><span style="color:blue;">THEN </span><span style="color:black;">10<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'mm-dd-yyyy' </span><span style="color:blue;">THEN </span><span style="color:black;">110<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'yy/mm/dd' </span><span style="color:blue;">THEN </span><span style="color:black;">11<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'yyyy/mm/dd' </span><span style="color:blue;">THEN </span><span style="color:black;">111<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'yymmdd' </span><span style="color:blue;">THEN </span><span style="color:black;">12<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'yyyymmdd' </span><span style="color:blue;">THEN </span><span style="color:black;">112<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'dd Mmm yyyy hh:mm:ss:Ms' </span><span style="color:blue;">THEN </span><span style="color:black;">113<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'hh:mm:ss:Ms' </span><span style="color:blue;">THEN </span><span style="color:black;">14<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'yyyy-mm-dd hh:mm:ss' </span><span style="color:blue;">THEN </span><span style="color:black;">120<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'yyyy-mm-dd hh:mm:ss.Ms' </span><span style="color:blue;">THEN </span><span style="color:black;">121<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'yyyy-mm-ddThh:mm:ss.Ms' </span><span style="color:blue;">THEN </span><span style="color:black;">126<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'dd Mmm yyyy hh:mm:ss:ms AM/PM' </span><span style="color:blue;">THEN </span><span style="color:black;">130<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'dd/mm/yy hh:mm:ss:ms AM/PM' </span><span style="color:blue;">THEN </span><span style="color:black;">131<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'RFC822' </span><span style="color:blue;">THEN </span><span style="color:black;">2<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'dd Mmm yyyy hh:mm' </span><span style="color:blue;">THEN </span><span style="color:black;">4<br />
</span><span style="color:blue;">ELSE </span><span style="color:black;">1 </span><span style="color:blue;">END<br />
SELECT </span><span style="color:#434343;">@ReturnedDate</span><span style="color:blue;">=</span><span style="color:red;">'error! unrecognised format ' </span><span style="color:gray;">+</span><span style="color:#434343;">@format</span><span style="color:gray;">+</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">),</span><span style="color:#434343;">@DateFormat</span><span style="color:gray;">)<br />
</span><span style="color:blue;">IF </span><span style="color:#434343;">@DateFormat</span><span style="color:gray;">&gt;=</span><span style="color:black;">0<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@ReturnedDate</span><span style="color:blue;">=</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">80</span><span style="color:gray;">),</span><span style="color:#434343;">@Date</span><span style="color:gray;">,</span><span style="color:#434343;">@DateFormat</span><span style="color:gray;">)<br />
</span><span style="color:green;">--check for favourite and custom formats that can be done quickly<br />
</span><span style="color:blue;">ELSE IF </span><span style="color:#434343;">@DateFormat</span><span style="color:blue;">=</span><span style="color:gray;">-</span><span style="color:black;">2</span><span style="color:green;">--then it is RFC822 format<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@ReturnedDate</span><span style="color:blue;">=</span><span style="color:magenta;">LEFT</span><span style="color:gray;">(</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:#434343;">@Date</span><span style="color:gray;">),</span><span style="color:black;">3</span><span style="color:gray;">) + </span><span style="color:red;">', ' </span><span style="color:gray;">+ </span><span style="color:magenta;">STUFF</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">,</span><span style="color:#434343;">@Date</span><span style="color:gray;">,</span><span style="color:black;">113</span><span style="color:gray;">),</span><span style="color:black;">21</span><span style="color:gray;">,</span><span style="color:black;">4</span><span style="color:gray;">,</span><span style="color:red;">' GMT'</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ELSE IF </span><span style="color:#434343;">@DateFormat</span><span style="color:blue;">=</span><span style="color:gray;">-</span><span style="color:black;">4</span><span style="color:green;">--then it is european day format with minutes<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@ReturnedDate</span><span style="color:blue;">=</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">17</span><span style="color:gray;">),</span><span style="color:#434343;">@Date</span><span style="color:gray;">,</span><span style="color:black;">113</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ELSE<br />
BEGIN<br />
SELECT </span><span style="color:#434343;">@Before</span><span style="color:blue;">=</span><span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:#434343;">@format</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@Format</span><span style="color:blue;">=</span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(</span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(</span><span style="color:magenta;">REPLACE</span><span style="color:gray;">( </span><span style="color:#434343;">@Format</span><span style="color:gray;">,</span><span style="color:red;">'AM/PM'</span><span style="color:gray;">,</span><span style="color:red;">'#'</span><span style="color:gray;">),</span><span style="color:red;">'AM'</span><span style="color:gray;">,</span><span style="color:red;">'#'</span><span style="color:gray;">),</span><span style="color:red;">'PM'</span><span style="color:gray;">,</span><span style="color:red;">'#'</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@TwelveHourClock</span><span style="color:blue;">=</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:#434343;">@Before </span><span style="color:gray;">&gt;</span><span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:#434343;">@format</span><span style="color:gray;">) </span><span style="color:blue;">THEN </span><span style="color:black;">109 </span><span style="color:blue;">ELSE </span><span style="color:black;">113 </span><span style="color:blue;">END</span><span style="color:gray;">, </span><span style="color:#434343;">@ReturnedDate</span><span style="color:blue;">=</span><span style="color:red;">''<br />
</span><span style="color:blue;">WHILE </span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:blue;">=</span><span style="color:black;">1</span><span style="color:gray;">)</span><span style="color:green;">--forever<br />
</span><span style="color:blue;">BEGIN<br />
SELECT </span><span style="color:#434343;">@pos</span><span style="color:blue;">=</span><span style="color:magenta;">PATINDEX</span><span style="color:gray;">(</span><span style="color:red;">'%[yqmidwhs:#]%'</span><span style="color:gray;">,</span><span style="color:#434343;">@format</span><span style="color:gray;">+</span><span style="color:red;">' '</span><span style="color:gray;">)<br />
</span><span style="color:blue;">IF </span><span style="color:#434343;">@pos</span><span style="color:blue;">=</span><span style="color:black;">0</span><span style="color:green;">--no more date format strings<br />
</span><span style="color:blue;">BEGIN<br />
SELECT </span><span style="color:#434343;">@ReturnedDate</span><span style="color:blue;">=</span><span style="color:#434343;">@ReturnedDate</span><span style="color:gray;">+</span><span style="color:#434343;">@format<br />
</span><span style="color:blue;">BREAK<br />
END<br />
IF </span><span style="color:#434343;">@pos</span><span style="color:gray;">&gt;</span><span style="color:black;">1</span><span style="color:green;">--some stuff to pass through first<br />
</span><span style="color:blue;">BEGIN<br />
SELECT </span><span style="color:#434343;">@escape</span><span style="color:blue;">=CHARINDEX </span><span style="color:gray;">(</span><span style="color:red;">'\'</span><span style="color:gray;">,</span><span style="color:#434343;">@Format</span><span style="color:gray;">+</span><span style="color:red;">'\'</span><span style="color:gray;">) </span><span style="color:green;">--is it a literal character that is escaped?<br />
</span><span style="color:blue;">IF </span><span style="color:#434343;">@escape</span><span style="color:gray;">&lt;</span><span style="color:#434343;">@pos </span><span style="color:blue;">BEGIN<br />
SET </span><span style="color:#434343;">@ReturnedDate</span><span style="color:blue;">=</span><span style="color:#434343;">@ReturnedDate</span><span style="color:gray;">+</span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@Format</span><span style="color:gray;">,</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:#434343;">@escape</span><span style="color:gray;">-</span><span style="color:black;">1</span><span style="color:gray;">) +</span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@format</span><span style="color:gray;">,</span><span style="color:#434343;">@escape</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;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@format</span><span style="color:blue;">=</span><span style="color:magenta;">RTRIM</span><span style="color:gray;">(</span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@Format</span><span style="color:gray;">,</span><span style="color:#434343;">@Escape</span><span style="color:gray;">+</span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:black;">80</span><span style="color:gray;">))<br />
</span><span style="color:blue;">CONTINUE<br />
END<br />
SET </span><span style="color:#434343;">@ReturnedDate</span><span style="color:blue;">=</span><span style="color:#434343;">@ReturnedDate</span><span style="color:gray;">+</span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@Format</span><span style="color:gray;">,</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:#434343;">@pos</span><span style="color:gray;">-</span><span style="color:black;">1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@format</span><span style="color:blue;">=</span><span style="color:magenta;">RTRIM</span><span style="color:gray;">(</span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@Format</span><span style="color:gray;">,</span><span style="color:#434343;">@pos</span><span style="color:gray;">,</span><span style="color:black;">80</span><span style="color:gray;">))<br />
</span><span style="color:blue;">END<br />
SELECT </span><span style="color:#434343;">@pos</span><span style="color:blue;">=</span><span style="color:magenta;">PATINDEX</span><span style="color:gray;">(</span><span style="color:red;">'%[^yqmidwhs:#]%'</span><span style="color:gray;">,</span><span style="color:#434343;">@format</span><span style="color:gray;">+</span><span style="color:red;">' '</span><span style="color:gray;">)</span><span style="color:green;">--get the end<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@ReturnedDate</span><span style="color:blue;">=</span><span style="color:#434343;">@ReturnedDate</span><span style="color:gray;">+</span><span style="color:green;">--'('+substring(@Format,1,@pos-1)+')'+<br />
</span><span style="color:magenta;">CASE SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@Format</span><span style="color:gray;">,</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:#434343;">@pos</span><span style="color:gray;">-</span><span style="color:black;">1</span><span style="color:gray;">)<br />
</span><span style="color:green;">--Mmmths as 1--12<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'M' </span><span style="color:blue;">THEN </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">2</span><span style="color:gray;">),</span><span style="color:magenta;">DATEPART</span><span style="color:gray;">(</span><span style="color:magenta;">MONTH</span><span style="color:gray;">,</span><span style="color:#434343;">@Date</span><span style="color:gray;">))<br />
</span><span style="color:green;">--Mmmths as 01--12<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Mm' </span><span style="color:blue;">THEN </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">2</span><span style="color:gray;">),</span><span style="color:#434343;">@Date</span><span style="color:gray;">,</span><span style="color:black;">101</span><span style="color:gray;">)<br />
</span><span style="color:green;">--Mmmths as Jan--Dec<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Mmm' </span><span style="color:blue;">THEN </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">3</span><span style="color:gray;">),</span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:magenta;">MONTH</span><span style="color:gray;">,</span><span style="color:#434343;">@Date</span><span style="color:gray;">))<br />
</span><span style="color:green;">--Mmmths as January--December<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Mmmm' </span><span style="color:blue;">THEN </span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:magenta;">MONTH</span><span style="color:gray;">,</span><span style="color:#434343;">@Date</span><span style="color:gray;">)<br />
</span><span style="color:green;">--Mmmths as the first letter of the Mmmth<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Mmmmm' </span><span style="color:blue;">THEN </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">),</span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:magenta;">MONTH</span><span style="color:gray;">,</span><span style="color:#434343;">@Date</span><span style="color:gray;">))<br />
</span><span style="color:green;">--Days as 1--31<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'D' </span><span style="color:blue;">THEN </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">2</span><span style="color:gray;">),</span><span style="color:magenta;">DATEPART</span><span style="color:gray;">(</span><span style="color:magenta;">DAY</span><span style="color:gray;">,</span><span style="color:#434343;">@Date</span><span style="color:gray;">))<br />
</span><span style="color:green;">--Days as 01--31<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Dd' </span><span style="color:blue;">THEN </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">2</span><span style="color:gray;">),</span><span style="color:#434343;">@date</span><span style="color:gray;">,</span><span style="color:black;">103</span><span style="color:gray;">)<br />
</span><span style="color:green;">--Days as Sun--Sat<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Ddd' </span><span style="color:blue;">THEN </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">3</span><span style="color:gray;">),</span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:black;">weekday</span><span style="color:gray;">,</span><span style="color:#434343;">@Date</span><span style="color:gray;">))<br />
</span><span style="color:green;">--Days as Sunday--Saturday<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Dddd' </span><span style="color:blue;">THEN </span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:black;">weekday</span><span style="color:gray;">,</span><span style="color:#434343;">@Date</span><span style="color:gray;">)<br />
</span><span style="color:green;">--Years as 00--99<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Yy' </span><span style="color:blue;">THEN </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">2</span><span style="color:gray;">),</span><span style="color:#434343;">@Date</span><span style="color:gray;">,</span><span style="color:black;">12</span><span style="color:gray;">)<br />
</span><span style="color:green;">--Years as 1900--9999<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Yyyy' </span><span style="color:blue;">THEN </span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:magenta;">YEAR</span><span style="color:gray;">,</span><span style="color:#434343;">@Date</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'hh:mm:ss' </span><span style="color:blue;">THEN </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">30</span><span style="color:gray;">),</span><span style="color:#434343;">@date</span><span style="color:gray;">,</span><span style="color:#434343;">@TwelveHourClock</span><span style="color:gray;">),</span><span style="color:black;">13</span><span style="color:gray;">,</span><span style="color:black;">8</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'hh:mm:ss:ms' </span><span style="color:blue;">THEN </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">30</span><span style="color:gray;">),</span><span style="color:#434343;">@date</span><span style="color:gray;">,</span><span style="color:#434343;">@TwelveHourClock</span><span style="color:gray;">),</span><span style="color:black;">13</span><span style="color:gray;">,</span><span style="color:black;">12</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'h:mm:ss' </span><span style="color:blue;">THEN </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">30</span><span style="color:gray;">),</span><span style="color:#434343;">@date</span><span style="color:gray;">,</span><span style="color:#434343;">@TwelveHourClock</span><span style="color:gray;">),</span><span style="color:black;">13</span><span style="color:gray;">,</span><span style="color:black;">8</span><span style="color:gray;">)<br />
</span><span style="color:green;">--tthe SQL Server BOL syntax, for compatibility<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'hh:mi:ss:mmm' </span><span style="color:blue;">THEN </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">30</span><span style="color:gray;">),</span><span style="color:#434343;">@date</span><span style="color:gray;">,</span><span style="color:#434343;">@TwelveHourClock</span><span style="color:gray;">),</span><span style="color:black;">13</span><span style="color:gray;">,</span><span style="color:black;">12</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'h:mm:ss:ms' </span><span style="color:blue;">THEN </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">30</span><span style="color:gray;">),</span><span style="color:#434343;">@date</span><span style="color:gray;">,</span><span style="color:#434343;">@TwelveHourClock</span><span style="color:gray;">),</span><span style="color:black;">13</span><span style="color:gray;">,</span><span style="color:black;">12</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'H:m:s' </span><span style="color:blue;">THEN </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(</span><span style="color:red;">':'</span><span style="color:gray;">+</span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">30</span><span style="color:gray;">), </span><span style="color:#434343;">@Date</span><span style="color:gray;">,</span><span style="color:#434343;">@TwelveHourClock</span><span style="color:gray;">),</span><span style="color:black;">13</span><span style="color:gray;">,</span><span style="color:black;">8</span><span style="color:gray;">),</span><span style="color:red;">':0'</span><span style="color:gray;">,</span><span style="color:red;">':'</span><span style="color:gray;">),</span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:black;">30</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'H:m:s:ms' </span><span style="color:blue;">THEN </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(</span><span style="color:red;">':'</span><span style="color:gray;">+</span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">30</span><span style="color:gray;">), </span><span style="color:#434343;">@Date</span><span style="color:gray;">,</span><span style="color:#434343;">@TwelveHourClock</span><span style="color:gray;">),</span><span style="color:black;">13</span><span style="color:gray;">,</span><span style="color:black;">12</span><span style="color:gray;">),</span><span style="color:red;">':0'</span><span style="color:gray;">,</span><span style="color:red;">':'</span><span style="color:gray;">),</span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:black;">30</span><span style="color:gray;">)<br />
</span><span style="color:green;">--Hours as 00--23<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'hh' </span><span style="color:blue;">THEN </span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(</span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">30</span><span style="color:gray;">), </span><span style="color:#434343;">@Date</span><span style="color:gray;">,</span><span style="color:#434343;">@TwelveHourClock</span><span style="color:gray;">),</span><span style="color:black;">13</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">),</span><span style="color:red;">' '</span><span style="color:gray;">,</span><span style="color:red;">'0'</span><span style="color:gray;">)<br />
</span><span style="color:green;">--Hours as 0--23<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'h' </span><span style="color:blue;">THEN </span><span style="color:magenta;">LTRIM</span><span style="color:gray;">(</span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">30</span><span style="color:gray;">), </span><span style="color:#434343;">@Date</span><span style="color:gray;">,</span><span style="color:#434343;">@TwelveHourClock</span><span style="color:gray;">),</span><span style="color:black;">13</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">))<br />
</span><span style="color:green;">--Minutes as 00--59<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'Mi' </span><span style="color:blue;">THEN </span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:black;">minute</span><span style="color:gray;">,</span><span style="color:#434343;">@date</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'mm' </span><span style="color:blue;">THEN </span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:black;">minute</span><span style="color:gray;">,</span><span style="color:#434343;">@date</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'m' </span><span style="color:blue;">THEN </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">2</span><span style="color:gray;">),</span><span style="color:magenta;">DATEPART</span><span style="color:gray;">(</span><span style="color:black;">minute</span><span style="color:gray;">,</span><span style="color:#434343;">@date</span><span style="color:gray;">))<br />
</span><span style="color:green;">--Seconds as 0--59<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'ss' </span><span style="color:blue;">THEN </span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:black;">second</span><span style="color:gray;">,</span><span style="color:#434343;">@date</span><span style="color:gray;">)<br />
</span><span style="color:green;">--Seconds as 0--59<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'S' </span><span style="color:blue;">THEN </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">2</span><span style="color:gray;">),</span><span style="color:magenta;">DATEPART</span><span style="color:gray;">(</span><span style="color:black;">second</span><span style="color:gray;">,</span><span style="color:#434343;">@date</span><span style="color:gray;">))<br />
</span><span style="color:green;">--AM/PM<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'ms' </span><span style="color:blue;">THEN </span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:black;">millisecond</span><span style="color:gray;">,</span><span style="color:#434343;">@date</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'mmm' </span><span style="color:blue;">THEN </span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:black;">millisecond</span><span style="color:gray;">,</span><span style="color:#434343;">@date</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'dy' </span><span style="color:blue;">THEN </span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:black;">dy</span><span style="color:gray;">,</span><span style="color:#434343;">@date</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'qq' </span><span style="color:blue;">THEN </span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:black;">qq</span><span style="color:gray;">,</span><span style="color:#434343;">@date</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'ww' </span><span style="color:blue;">THEN </span><span style="color:magenta;">DATENAME</span><span style="color:gray;">(</span><span style="color:black;">ww</span><span style="color:gray;">,</span><span style="color:#434343;">@date</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:red;">'#' </span><span style="color:blue;">THEN </span><span style="color:magenta;">REVERSE</span><span style="color:gray;">(</span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">REVERSE</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">26</span><span style="color:gray;">), </span><span style="color:#434343;">@date</span><span style="color:gray;">,</span><span style="color:black;">109</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;">))<br />
</span><span style="color:blue;">ELSE<br />
</span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@Format</span><span style="color:gray;">,</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:#434343;">@pos</span><span style="color:gray;">-</span><span style="color:black;">1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">END<br />
SET </span><span style="color:#434343;">@format</span><span style="color:blue;">=</span><span style="color:magenta;">RTRIM</span><span style="color:gray;">(</span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@Format</span><span style="color:gray;">,</span><span style="color:#434343;">@pos</span><span style="color:gray;">,</span><span style="color:black;">80</span><span style="color:gray;">))<br />
</span><span style="color:blue;">END<br />
END<br />
RETURN </span><span style="color:#434343;">@ReturnedDate<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">[dbo].[ufsFormat] </span><span style="color:gray;">(</span><span style="color:red;">'8/7/2008'</span><span style="color:gray;">, </span><span style="color:red;">'mm/dd/yy'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">[dbo].[ufsFormat] </span><span style="color:gray;">(</span><span style="color:red;">'8/7/2008'</span><span style="color:gray;">, </span><span style="color:red;">'hh:mm:ss'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">[dbo].[ufsFormat] </span><span style="color:gray;">(</span><span style="color:red;">'8/7/2008'</span><span style="color:gray;">, </span><span style="color:red;">'mmm'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">[dbo].[ufsFormat] </span><span style="color:gray;">(</span><span style="color:red;">'8/7/2008'</span><span style="color:gray;">, </span><span style="color:red;">'Mmm dd yyyy hh:mm:ss:ms AM/PM'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">[dbo].[ufsFormat] </span><span style="color:gray;">(</span><span style="color:red;">'8/7/2008'</span><span style="color:gray;">, </span><span style="color:red;">'#'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/dateudf.jpg" alt="" width="500" height="407" /></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> , <a href="http://blog.sqlauthority.com/2008/08/01/sql-server-2008-get-current-system-date-time/#comment-41412" target="_blank">Nanda</a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/820/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/820/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/820/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/820/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/820/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/820/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/820/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/820/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/820/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/820/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/820/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/820/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/820/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/820/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/820/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/820/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=820&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/08/14/sql-server-get-date-time-in-any-format-udf-user-defined-functions/feed/</wfw:commentRss>
		<slash:comments>34</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/dateudf.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; Get Current System Date Time</title>
		<link>http://blog.sqlauthority.com/2008/08/01/sql-server-2008-get-current-system-date-time/</link>
		<comments>http://blog.sqlauthority.com/2008/08/01/sql-server-2008-get-current-system-date-time/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 01:30:06 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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://sqlauthority.wordpress.com/?p=764</guid>
		<description><![CDATA[How to get current system date time in SQL Server? First thing which comes to many users is using following script. SELECT GETDATE() AS CurrentDateTime Above method is not the only method to retrieve the current system date time for SQL Server. SQL Server 2008 has many different function which provides current system date time [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=764&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">How to get current system date time in SQL Server?</p>
<p style="text-align:justify;">First thing which comes to many users is using following script.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:blue;">AS </span><span style="color:black;">CurrentDateTime</span></code></p>
<p style="text-align:justify;">Above method is not the only method to retrieve the current system date time for SQL Server. SQL Server 2008 has many different function which provides current system date time in different format and little difference in details.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:red;">'SYSDATETIME' </span><span style="color:blue;">AS </span><span style="color:black;">FunctionName</span><span style="color:gray;">, </span><span style="color:black;">SYSDATETIME</span><span style="color:gray;">() </span><span style="color:blue;">AS </span><span style="color:black;">DateTimeFormat<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'SYSDATETIMEOFFSET'</span><span style="color:gray;">, </span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">()<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'SYSUTCDATETIME'</span><span style="color:gray;">, </span><span style="color:black;">SYSUTCDATETIME</span><span style="color:gray;">()<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'CURRENT_TIMESTAMP'</span><span style="color:gray;">, </span><span style="color:magenta;">CURRENT_TIMESTAMP<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'GETDATE'</span><span style="color:gray;">, </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:red;">'GETUTCDATE'</span><span style="color:gray;">, </span><span style="color:magenta;">GETUTCDATE</span><span style="color:gray;">() </span></code>
</p>
<p style="text-align:justify;">In SQL Server 2008, use any of the above function depending on your need.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/datetime2008.gif" alt="" width="422" height="406" /></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 /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/764/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/764/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/764/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/764/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/764/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/764/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/764/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/764/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/764/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/764/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/764/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/764/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/764/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/764/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/764/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/764/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=764&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/08/01/sql-server-2008-get-current-system-date-time/feed/</wfw:commentRss>
		<slash:comments>33</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/datetime2008.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; Find Current System Date Time and Time Offset</title>
		<link>http://blog.sqlauthority.com/2008/07/31/sql-server-2008-find-current-system-date-time-and-time-offset/</link>
		<comments>http://blog.sqlauthority.com/2008/07/31/sql-server-2008-find-current-system-date-time-and-time-offset/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 01:30:23 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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://sqlauthority.wordpress.com/?p=756</guid>
		<description><![CDATA[If you want to find current datetime in SQL Server I suggest to read the following post : SQL SERVER &#8211; Retrieve Current Date Time in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()} This post is related to new feature available in SQL Server 2008. In SQL Server 2008 there is a function which provides current [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=756&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">If you want to find current datetime in SQL Server I suggest to read the following post :</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2007/06/21/sql-server-retrieve-current-date-time-in-sql-server-current_timestamp-getdate-fn-now/" target="_blank">SQL SERVER &#8211; Retrieve Current Date Time in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}</a></p>
<p style="text-align:justify;">This post is related to new feature available in SQL Server 2008. In SQL Server 2008 there is a function which provides current offset of the system from GMT time as well. Basically it shows the system datetime with offset. I think this can be useful in some of the instances where SQL Server are depending on the time offset.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">SYSDATETIMEOFFSET</span><span style="color:gray;">() </span><span style="color:blue;">AS </span><span style="color:red;">'Windows System Time'<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/timeoffset.gif" alt="" width="461" height="522" /></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 /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/756/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/756/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/756/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=756&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/07/31/sql-server-2008-find-current-system-date-time-and-time-offset/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/timeoffset.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; SQL Server Start Time</title>
		<link>http://blog.sqlauthority.com/2008/06/17/sql-server-2008-sql-server-start-time/</link>
		<comments>http://blog.sqlauthority.com/2008/06/17/sql-server-2008-sql-server-start-time/#comments</comments>
		<pubDate>Tue, 17 Jun 2008 01:30:29 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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://sqlauthority.wordpress.com/?p=650</guid>
		<description><![CDATA[I have been playing with SQL Server 2008 recently. There are many new features which SQL Server 2008 have. One of the interesting addition to SQL Server 2008 is system table field which records when SQL Server was started. This field has data type as datetime that is why it is precise to 3 milisecond. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=650&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I have been playing with SQL Server 2008 recently. There are many new features which SQL Server 2008 have. One of the interesting addition to SQL Server 2008 is system table field which records when SQL Server was started. This field has data type as datetime that is why it is precise to 3 milisecond.</p>
<p style="text-align:justify;">Note : This will not work with SQL Server 2005 or earlier version. This works with SQL Server 2008 only.</p>
<p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">sqlserver_start_time<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_os_sys_info</span></code></p>
<p style="text-align:justify;"><strong>ResultSet:</strong><br />
sqlserver_start_time<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
2008-06-27 20:51:53.317</p>
<p>Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/650/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/650/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/650/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/650/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/650/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/650/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/650/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/650/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/650/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/650/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/650/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/650/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/650/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/650/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/650/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/650/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=650&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/06/17/sql-server-2008-sql-server-start-time/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>
	</item>
		<item>
		<title>SQL SERVER &#8211; SQL SERVER &#8211; UDF &#8211; Get the Day of the Week Function &#8211; Part 4</title>
		<link>http://blog.sqlauthority.com/2008/06/01/sql-server-sql-server-udf-get-the-day-of-the-week-function-part-4/</link>
		<comments>http://blog.sqlauthority.com/2008/06/01/sql-server-sql-server-udf-get-the-day-of-the-week-function-part-4/#comments</comments>
		<pubDate>Sun, 01 Jun 2008 01:30:38 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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://sqlauthority.wordpress.com/?p=623</guid>
		<description><![CDATA[I have been asked many times when there is DATENAME function available why do I go in exercise of writing UDF For the getting the day of the week. Answer is : I just like it! SELECT DATENAME(dw, GETDATE()) Reference : Pinal Dave (http://blog.SQLAuthority.com)<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=623&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I have been asked many times when there is DATENAME function available why do I go in exercise of writing UDF For the getting the day of the week. Answer is : I just like it!</p>
<pre 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:black;">dw</span><span style="color:gray;">, </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">())</span></code></pre>
<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 /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/623/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/623/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/623/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/623/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/623/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/623/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/623/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/623/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/623/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/623/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/623/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/623/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/623/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/623/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/623/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/623/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=623&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/06/01/sql-server-sql-server-udf-get-the-day-of-the-week-function-part-4/feed/</wfw:commentRss>
		<slash:comments>5</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; SQL SERVER &#8211; UDF &#8211; Get the Day of the Week Function &#8211; Part 3</title>
		<link>http://blog.sqlauthority.com/2008/05/27/sql-server-sql-server-udf-get-the-day-of-the-week-function-part-3/</link>
		<comments>http://blog.sqlauthority.com/2008/05/27/sql-server-sql-server-udf-get-the-day-of-the-week-function-part-3/#comments</comments>
		<pubDate>Tue, 27 May 2008 01:30:48 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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://sqlauthority.wordpress.com/?p=618</guid>
		<description><![CDATA[Datetime functions and stored procedures always interests me. Nanda Kumar has suggested modification to previous written article about SQL SERVER &#8211; SQL SERVER &#8211; UDF &#8211; Get the Day of the Week Function &#8211; Part 2. He has improved on UDF. CREATE FUNCTION dbo.udf_DayOfWeek(@dtDate DATETIME) RETURNS VARCHAR(10) AS BEGIN DECLARE @rtDayofWeek VARCHAR(10) DECLARE @weekDay INT [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=618&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Datetime functions and stored procedures always interests me. <a href="http://blog.sqlauthority.com/2008/05/23/sql-server-sql-server-udf-get-the-day-of-the-week-function-part-2/#comment-39088" target="_blank">Nanda Kumar</a> has suggested modification to previous written article about <strong><a href="http://blog.sqlauthority.com/2008/05/23/sql-server-sql-server-udf-get-the-day-of-the-week-function-part-2/" target="_blank">SQL SERVER &#8211; SQL SERVER &#8211; UDF &#8211; Get the Day of the Week Function &#8211; Part 2</a></strong>. He has improved on UDF.</p>
<p><code style="font-size:12px;"><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">dbo.udf_DayOfWeek</span><span style="color:gray;">(</span><span style="color:#434343;">@dtDate </span><span style="color:black;">DATETIME</span><span style="color:gray;">)<br />
</span><span style="color:blue;">RETURNS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
BEGIN<br />
DECLARE </span><span style="color:#434343;">@rtDayofWeek </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@weekDay </span><span style="color:blue;">INT<br />
</span><span style="color:green;">----Here I have subtracted 7 For keeping Sunday as the First day like wise for Monday we need to subtract 2 and so on<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@weekDay</span><span style="color:blue;">=</span><span style="color:gray;">((</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:#434343;">@dtDate</span><span style="color:gray;">)+</span><span style="color:#434343;">@@DATEFIRST</span><span style="color:gray;">-</span><span style="color:black;">7</span><span style="color:gray;">)%</span><span style="color:black;">7</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@rtDayofWeek </span><span style="color:blue;">= </span><span style="color:magenta;">CASE </span><span style="color:#434343;">@weekDay<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:red;">'Sunday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">2 </span><span style="color:blue;">THEN </span><span style="color:red;">'Monday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">3 </span><span style="color:blue;">THEN </span><span style="color:red;">'Tuesday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">4 </span><span style="color:blue;">THEN </span><span style="color:red;">'Wednesday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">5 </span><span style="color:blue;">THEN </span><span style="color:red;">'Thursday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">6 </span><span style="color:blue;">THEN </span><span style="color:red;">'Friday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">0 </span><span style="color:blue;">THEN </span><span style="color:red;">'Saturday'<br />
</span><span style="color:blue;">END<br />
RETURN </span><span style="color:gray;">(</span><span style="color:#434343;">@rtDayofWeek</span><span style="color:gray;">)<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">dbo.udf_dayofweek</span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">())</span></code></p>
<p>Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong> , <a href="http://blog.sqlauthority.com/2008/05/23/sql-server-sql-server-udf-get-the-day-of-the-week-function-part-2/#comment-39088" target="_blank">Nanda Kumar</a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/618/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/618/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/618/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=618&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/05/27/sql-server-sql-server-udf-get-the-day-of-the-week-function-part-3/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>
	</item>
		<item>
		<title>SQL SERVER &#8211; SQL SERVER &#8211; UDF &#8211; Get the Day of the Week Function &#8211; Part 2</title>
		<link>http://blog.sqlauthority.com/2008/05/23/sql-server-sql-server-udf-get-the-day-of-the-week-function-part-2/</link>
		<comments>http://blog.sqlauthority.com/2008/05/23/sql-server-sql-server-udf-get-the-day-of-the-week-function-part-2/#comments</comments>
		<pubDate>Fri, 23 May 2008 01:30:51 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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://sqlauthority.wordpress.com/?p=613</guid>
		<description><![CDATA[I have written article about SQL SERVER &#8211; UDF &#8211; Get the Day of the Week Function. I have received good modified script from reader Mihir Popat has suggested another code where Sunday does not have to be necessary the first day of the week. CREATE FUNCTION dbo.udf_DayOfWeek(@dtDate DATETIME) RETURNS VARCHAR(10) AS BEGIN DECLARE @rtDayofWeek [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=613&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I have written article about  <strong><a href="http://blog.sqlauthority.com/2007/07/23/sql-server-udf-get-the-day-of-the-week-function/" target="_blank">SQL SERVER &#8211; UDF &#8211; Get the Day of the Week Function</a></strong>. I have received good modified script from reader <a href="http://blog.sqlauthority.com/2007/07/23/sql-server-udf-get-the-day-of-the-week-function/#comment-38946" target="_blank">Mihir Popat</a> has suggested another code where Sunday does not have to be necessary the first day of the week.<br />
<code style="font-size:12px;"><span style="color:blue;"><br />
CREATE FUNCTION </span><span style="color:black;">dbo.udf_DayOfWeek</span><span style="color:gray;">(</span><span style="color:#434343;">@dtDate </span><span style="color:black;">DATETIME</span><span style="color:gray;">)<br />
</span><span style="color:blue;">RETURNS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
BEGIN<br />
DECLARE </span><span style="color:#434343;">@rtDayofWeek </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@weekDay </span><span style="color:blue;">INT<br />
</span><span style="color:green;">-- Here I have subtracted 7 For keeping Sunday as the First day<br />
-- like wise for Monday we need to subtract 2 and so on<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@weekDay </span><span style="color:blue;">= </span><span style="color:gray;">((</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:magenta;">GETDATE</span><span style="color:gray;">())+</span><span style="color:#434343;">@@DATEFIRST</span><span style="color:gray;">-</span><span style="color:black;">7</span><span style="color:gray;">)%</span><span style="color:black;">7</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@rtDayofWeek </span><span style="color:blue;">= </span><span style="color:magenta;">CASE </span><span style="color:#434343;">@weekDay<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:red;">'Sunday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">2 </span><span style="color:blue;">THEN </span><span style="color:red;">'Monday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">3 </span><span style="color:blue;">THEN </span><span style="color:red;">'Tuesday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">4 </span><span style="color:blue;">THEN </span><span style="color:red;">'Wednesday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">5 </span><span style="color:blue;">THEN </span><span style="color:red;">'Thursday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">6 </span><span style="color:blue;">THEN </span><span style="color:red;">'Friday'<br />
</span><span style="color:blue;">WHEN </span><span style="color:black;">7 </span><span style="color:blue;">THEN </span><span style="color:red;">'Saturday'<br />
</span><span style="color:blue;">END<br />
RETURN </span><span style="color:gray;">(</span><span style="color:#434343;">@rtDayofWeek</span><span style="color:gray;">)<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">dbo.udf_DayOfWeek</span><span style="color:gray;">(</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">())</span></code></p>
<p>Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong>, <a href="http://blog.sqlauthority.com/2007/07/23/sql-server-udf-get-the-day-of-the-week-function/#comment-38946" target="_blank">Mihir Popat</a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/613/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/613/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/613/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/613/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/613/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/613/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/613/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/613/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/613/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/613/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/613/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/613/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/613/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/613/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/613/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/613/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=613&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/05/23/sql-server-sql-server-udf-get-the-day-of-the-week-function-part-2/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>
	</item>
		<item>
		<title>SQL SERVER &#8211; UDF to Return a Calendar for Any Date for Any Year</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/</link>
		<comments>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comments</comments>
		<pubDate>Wed, 20 Feb 2008 01:30:32 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></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://sqlauthority.wordpress.com/?p=512</guid>
		<description><![CDATA[It gives me great pleasure to write articles like today&#8217;s one because I have received great comment from one of regular reader who has taken UDF written by me and created another UDF using that UDF which enhances functionality of it. I had written previous article about SQL SERVER &#8211; UDF &#8211; Function to Display [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=512&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">It gives me great pleasure to write articles like today&#8217;s one because I have received great comment from one of regular reader who has taken UDF written by me and created another UDF using that UDF which enhances functionality of it. I had written previous article about <a href="http://blog.sqlauthority.com/2007/06/08/sql-server-udf-function-to-display-current-week-date-and-day-weekly-calendar/" target="_blank">SQL SERVER &#8211; UDF &#8211; Function to Display Current Week Date and Day &#8211; Weekly Calendar</a>. Reader of this blog and great SQL expert Dan Golden has wrote another UDF which uses UDF written by me. I thank Dan Golden for <a href="http://blog.sqlauthority.com/2007/06/08/sql-server-udf-function-to-display-current-week-date-and-day-weekly-calendar/#comment-33859" target="_blank">his contribution</a> to this blog. I have modified his function a bit to remove error when it was missing displaying first or last week in few cases, however I will give full credit to him for his thought.</p>
<p style="text-align:justify;">I also encourage my other readers to submit their articles to me, if I think your article will benefit other readers I will publish it here with your name. Do not send me article if it is not written by you.</p>
<p style="text-align:justify;"><strong>Step 1 : Create UDF to display current week date and day &#8211; Weekly Calender.</strong><br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">dbo.DisplayCurrentWeekDays<br />
</span><span style="color:gray;">(</span><span style="color:#434343;">@today </span><span style="color:black;">SMALLDATETIME</span><span style="color:gray;">)<br />
</span><span style="color:blue;">RETURNS </span><span style="color:#434343;">@WeekDateDay </span><span style="color:blue;">TABLE<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">Sunday SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">Monday SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">Tuesday SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">Wednesday SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">Thursday SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">Friday SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">Saturday SMALLDATETIME<br />
</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
</span><span style="color:green;">/*<br />
Purpose: To return the weekly calendar for the week any date<br />
By: Pinal Dave -- SQLAuthority.com<br />
On: 6/8/2007<br />
*/<br />
</span><span style="color:blue;">BEGIN<br />
DECLARE </span><span style="color:#434343;">@day </span><span style="color:blue;">INT<br />
SET </span><span style="color:#434343;">@today </span><span style="color:blue;">= </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">), </span><span style="color:#434343;">@today</span><span style="color:gray;">, </span><span style="color:black;">101</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">SMALLDATETIME</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@day </span><span style="color:blue;">= </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:#434343;">@today</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@WeekDateDay </span><span style="color:gray;">(</span><span style="color:black;">Sunday</span><span style="color:gray;">, </span><span style="color:black;">Monday</span><span style="color:gray;">,<br />
</span><span style="color:black;">Tuesday</span><span style="color:gray;">, </span><span style="color:black;">Wednesday</span><span style="color:gray;">, </span><span style="color:black;">Thursday</span><span style="color:gray;">, </span><span style="color:black;">Friday</span><span style="color:gray;">, </span><span style="color:black;">Saturday</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">dd</span><span style="color:gray;">, </span><span style="color:black;">1 </span><span style="color:gray;">- </span><span style="color:#434343;">@day</span><span style="color:gray;">, </span><span style="color:#434343;">@today</span><span style="color:gray;">) </span><span style="color:black;">Sunday</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">dd</span><span style="color:gray;">, </span><span style="color:black;">2 </span><span style="color:gray;">- </span><span style="color:#434343;">@day</span><span style="color:gray;">, </span><span style="color:#434343;">@today</span><span style="color:gray;">) </span><span style="color:black;">Monday</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">dd</span><span style="color:gray;">, </span><span style="color:black;">3 </span><span style="color:gray;">- </span><span style="color:#434343;">@day</span><span style="color:gray;">, </span><span style="color:#434343;">@today</span><span style="color:gray;">) </span><span style="color:black;">Tuesday</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">dd</span><span style="color:gray;">, </span><span style="color:black;">4 </span><span style="color:gray;">- </span><span style="color:#434343;">@day</span><span style="color:gray;">, </span><span style="color:#434343;">@today</span><span style="color:gray;">) </span><span style="color:black;">Wednesday</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">dd</span><span style="color:gray;">, </span><span style="color:black;">5 </span><span style="color:gray;">- </span><span style="color:#434343;">@day</span><span style="color:gray;">, </span><span style="color:#434343;">@today</span><span style="color:gray;">) </span><span style="color:black;">Thursday</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">dd</span><span style="color:gray;">, </span><span style="color:black;">6 </span><span style="color:gray;">- </span><span style="color:#434343;">@day</span><span style="color:gray;">, </span><span style="color:#434343;">@today</span><span style="color:gray;">) </span><span style="color:black;">Friday</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">dd</span><span style="color:gray;">, </span><span style="color:black;">7 </span><span style="color:gray;">- </span><span style="color:#434343;">@day</span><span style="color:gray;">, </span><span style="color:#434343;">@today</span><span style="color:gray;">) </span><span style="color:black;">Saturday<br />
</span><span style="color:blue;">RETURN<br />
END<br />
</span><span style="color:black;">GO<br />
</span></code><br />
<strong>Step 2 :  Create UDF to display current month date and day &#8211; Monthly Calender</strong><br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">dbo.GetSQLcalendar</span><span style="color:gray;">(</span><span style="color:#434343;">@WhatDate </span><span style="color:black;">SMALLDATETIME</span><span style="color:gray;">)<br />
</span><span style="color:blue;">RETURNS </span><span style="color:#434343;">@WeekDateDay </span><span style="color:blue;">TABLE<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">Sunday SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">Monday SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">Tuesday SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">Wednesday SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">Thursday SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">Friday SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">Saturday SMALLDATETIME<br />
</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
BEGIN<br />
</span><span style="color:green;">/*<br />
Purpose: To return a calendar fOR whatever MONTH you want,<br />
driven by the date you pick AS a variable.<br />
By: Dan Golden<br />
On: 2/20/2008<br />
*/<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@FourWeeksAgo </span><span style="color:black;">SMALLDATETIME<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@FourWeeksAgo </span><span style="color:blue;">= </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">,-</span><span style="color:black;">28</span><span style="color:gray;">,</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@ThreeWeeksAgo </span><span style="color:black;">SMALLDATETIME<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@ThreeWeeksAgo </span><span style="color:blue;">= </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">,-</span><span style="color:black;">21</span><span style="color:gray;">,</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@TwoWeeksAgo </span><span style="color:black;">SMALLDATETIME<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@TwoWeeksAgo </span><span style="color:blue;">= </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">,-</span><span style="color:black;">14</span><span style="color:gray;">,</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@PreviousWeek </span><span style="color:black;">SMALLDATETIME<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@PreviousWeek </span><span style="color:blue;">= </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">,-</span><span style="color:black;">7</span><span style="color:gray;">,</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@ThisWeek </span><span style="color:black;">SMALLDATETIME<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@ThisWeek </span><span style="color:blue;">= </span><span style="color:gray;">(</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@NextWeek </span><span style="color:black;">SMALLDATETIME<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@NextWeek </span><span style="color:blue;">= </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">,</span><span style="color:black;">7</span><span style="color:gray;">,</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@LastWeek </span><span style="color:black;">SMALLDATETIME<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@LastWeek </span><span style="color:blue;">= </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">,</span><span style="color:black;">14</span><span style="color:gray;">,</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@AfterLastWeek </span><span style="color:black;">SMALLDATETIME<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@AfterLastWeek </span><span style="color:blue;">= </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">,</span><span style="color:black;">21</span><span style="color:gray;">,</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@TwoAfterLastWeek </span><span style="color:black;">SMALLDATETIME<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@TwoAfterLastWeek </span><span style="color:blue;">= </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">,</span><span style="color:black;">28</span><span style="color:gray;">,</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@WeekDateDay </span><span style="color:gray;">(</span><span style="color:black;">Sunday</span><span style="color:gray;">, </span><span style="color:black;">Monday</span><span style="color:gray;">, </span><span style="color:black;">Tuesday</span><span style="color:gray;">,<br />
</span><span style="color:black;">Wednesday</span><span style="color:gray;">, </span><span style="color:black;">Thursday</span><span style="color:gray;">, </span><span style="color:black;">Friday</span><span style="color:gray;">, </span><span style="color:black;">Saturday</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM<br />
</span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">CASE MONTH</span><span style="color:gray;">(</span><span style="color:black;">calendar.Sunday</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:magenta;">MONTH</span><span style="color:gray;">(</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">THEN </span><span style="color:black;">calendar.Sunday<br />
</span><span style="color:blue;">ELSE </span><span style="color:gray;">NULL </span><span style="color:blue;">END AS </span><span style="color:red;">'Sunday'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE MONTH</span><span style="color:gray;">(</span><span style="color:black;">calendar.Monday</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:magenta;">MONTH</span><span style="color:gray;">(</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">THEN </span><span style="color:black;">calendar.Monday<br />
</span><span style="color:blue;">ELSE </span><span style="color:gray;">NULL </span><span style="color:blue;">END AS </span><span style="color:red;">'Monday'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE MONTH</span><span style="color:gray;">(</span><span style="color:black;">calendar.Tuesday</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:magenta;">MONTH</span><span style="color:gray;">(</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">THEN </span><span style="color:black;">calendar.Tuesday<br />
</span><span style="color:blue;">ELSE </span><span style="color:gray;">NULL </span><span style="color:blue;">END AS </span><span style="color:red;">'Tuesday'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE MONTH</span><span style="color:gray;">(</span><span style="color:black;">calendar.Wednesday</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:magenta;">MONTH</span><span style="color:gray;">(</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">THEN </span><span style="color:black;">calendar.Wednesday<br />
</span><span style="color:blue;">ELSE </span><span style="color:gray;">NULL </span><span style="color:blue;">END AS </span><span style="color:red;">'Wednesday'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE MONTH</span><span style="color:gray;">(</span><span style="color:black;">calendar.Thursday</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:magenta;">MONTH</span><span style="color:gray;">(</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">THEN </span><span style="color:black;">calendar.Thursday<br />
</span><span style="color:blue;">ELSE </span><span style="color:gray;">NULL </span><span style="color:blue;">END AS </span><span style="color:red;">'Thursday'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE MONTH</span><span style="color:gray;">(</span><span style="color:black;">calendar.Friday</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:magenta;">MONTH</span><span style="color:gray;">(</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">THEN </span><span style="color:black;">calendar.Friday<br />
</span><span style="color:blue;">ELSE </span><span style="color:gray;">NULL </span><span style="color:blue;">END AS </span><span style="color:red;">'Friday'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE MONTH</span><span style="color:gray;">(</span><span style="color:black;">calendar.Saturday</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHEN </span><span style="color:magenta;">MONTH</span><span style="color:gray;">(</span><span style="color:#434343;">@WhatDate</span><span style="color:gray;">)<br />
</span><span style="color:blue;">THEN </span><span style="color:black;">calendar.Saturday<br />
</span><span style="color:blue;">ELSE </span><span style="color:gray;">NULL </span><span style="color:blue;">END AS </span><span style="color:red;">'Saturday'<br />
</span><span style="color:blue;">FROM<br />
</span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.DisplayCurrentWeekDays</span><span style="color:gray;">(</span><span style="color:#434343;">@FourWeeksAgo</span><span style="color:gray;">)<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.DisplayCurrentWeekDays</span><span style="color:gray;">(</span><span style="color:#434343;">@ThreeWeeksAgo</span><span style="color:gray;">)<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.DisplayCurrentWeekDays</span><span style="color:gray;">(</span><span style="color:#434343;">@TwoWeeksAgo</span><span style="color:gray;">)<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.DisplayCurrentWeekDays</span><span style="color:gray;">(</span><span style="color:#434343;">@PreviousWeek</span><span style="color:gray;">)<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.DisplayCurrentWeekDays</span><span style="color:gray;">(</span><span style="color:#434343;">@ThisWeek</span><span style="color:gray;">)<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.DisplayCurrentWeekDays</span><span style="color:gray;">(</span><span style="color:#434343;">@NextWeek</span><span style="color:gray;">)<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.DisplayCurrentWeekDays</span><span style="color:gray;">(</span><span style="color:#434343;">@LastWeek</span><span style="color:gray;">)<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.DisplayCurrentWeekDays</span><span style="color:gray;">(</span><span style="color:#434343;">@AfterLastWeek</span><span style="color:gray;">)<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.DisplayCurrentWeekDays</span><span style="color:gray;">(</span><span style="color:#434343;">@TwoAfterLastWeek</span><span style="color:gray;">)<br />
) </span><span style="color:black;">calendar<br />
</span><span style="color:gray;">) </span><span style="color:black;">dates<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">dates.sunday </span><span style="color:blue;">IS </span><span style="color:gray;">NOT NULL<br />
OR </span><span style="color:black;">dates.monday </span><span style="color:blue;">IS </span><span style="color:gray;">NOT NULL<br />
OR </span><span style="color:black;">dates.tuesday </span><span style="color:blue;">IS </span><span style="color:gray;">NOT NULL<br />
OR </span><span style="color:black;">dates.wednesday </span><span style="color:blue;">IS </span><span style="color:gray;">NOT NULL<br />
OR </span><span style="color:black;">dates.thursday </span><span style="color:blue;">IS </span><span style="color:gray;">NOT NULL<br />
OR </span><span style="color:black;">dates.friday </span><span style="color:blue;">IS </span><span style="color:gray;">NOT NULL<br />
OR </span><span style="color:black;">dates.saturday </span><span style="color:blue;">IS </span><span style="color:gray;">NOT NULL<br />
</span><span style="color:blue;">RETURN<br />
END<br />
</span><span style="color:black;">GO<br />
</span></code><br />
<strong>Step 3 : Use  recently created UDF to  get the current month &#8216;s calender. You can pass any date to this calender and it will return that month and years calender.</strong><br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@WhatDay </span><span style="color:black;">SMALLDATETIME<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@WhatDay </span><span style="color:blue;">= </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(); </span><span style="color:green;">-- you can also put here any date like 07/01/1990<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">20</span><span style="color:gray;">), </span><span style="color:black;">Sunday</span><span style="color:gray;">, </span><span style="color:black;">101</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:red;">'Sunday'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">20</span><span style="color:gray;">), </span><span style="color:black;">Monday</span><span style="color:gray;">, </span><span style="color:black;">101</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:red;">'Monday'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">20</span><span style="color:gray;">), </span><span style="color:black;">Tuesday</span><span style="color:gray;">, </span><span style="color:black;">101</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:red;">'Tuesday'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">20</span><span style="color:gray;">), </span><span style="color:black;">Wednesday</span><span style="color:gray;">, </span><span style="color:black;">101</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:red;">'Wednesday'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">20</span><span style="color:gray;">), </span><span style="color:black;">Thursday</span><span style="color:gray;">, </span><span style="color:black;">101</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:red;">'Thursday'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">20</span><span style="color:gray;">), </span><span style="color:black;">Friday</span><span style="color:gray;">, </span><span style="color:black;">101</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:red;">'Friday'</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">20</span><span style="color:gray;">), </span><span style="color:black;">Saturday</span><span style="color:gray;">, </span><span style="color:black;">101</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:red;">'Saturday'<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.GetSQLcalendar</span><span style="color:gray;">(</span><span style="color:#434343;">@WhatDay</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span></code><br />
Above query will return you monthly calender for date passed to function.
</p>
<p style="text-align:justify;"><img src="http://www.pinaldave.com/bimg/calendar.gif" alt="" width="500" height="323" /></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> , <a href="http://blog.sqlauthority.com/2007/06/08/sql-server-udf-function-to-display-current-week-date-and-day-weekly-calendar/#comment-33859" target="_blank">Dan Golden</a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/512/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/512/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/512/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=512&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/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/calendar.gif" medium="image" />
	</item>
	</channel>
</rss>
