<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Chris Crowe</title>
        <link>http://blog.crowe.co.nz/Default.aspx</link>
        <description />
        <language>en-NZ</language>
        <copyright>Chris Crowe</copyright>
        <generator>Subtext Version 2.5.0.3</generator>
        <image>
            <title>Chris Crowe</title>
            <url>http://blog.crowe.co.nz/images/RSS2Image.gif</url>
            <link>http://blog.crowe.co.nz/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>chris</title>
            <link>http://blog.crowe.co.nz/archive/2012/03/15/chris.aspx</link>
            <description>&lt;p&gt;chris&lt;/p&gt;&lt;img src="http://blog.crowe.co.nz/aggbug/934.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Chris Crowe</dc:creator>
            <guid>http://blog.crowe.co.nz/archive/2012/03/15/chris.aspx</guid>
            <pubDate>Wed, 14 Mar 2012 17:59:05 GMT</pubDate>
            <comments>http://blog.crowe.co.nz/archive/2012/03/15/chris.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://blog.crowe.co.nz/comments/commentRss/934.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Microsoft Office Outlook has encountered a problem and needs to close when opening email messages</title>
            <link>http://blog.crowe.co.nz/archive/2010/11/03/Outlook-2010-crashes-when-opening-some-email-messages.aspx</link>
            <description>&lt;p&gt;I recently has a user who found that email messages from specific people would cause her Outlook 2010 to crash 100% of the time. Most peoples emails were perfectly fine and others were not.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;There were lots of entries in the Windows Event Log like the following&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Faulting application name: OUTLOOK.EXE, version: 14.0.4760.1000, time stamp: 0x4ba8fefd &lt;/p&gt;    &lt;p&gt;Faulting module name: USP10.DLL, version: 1.626.7600.20602, time stamp: 0x4b304c27 &lt;/p&gt;    &lt;p&gt;Exception code: 0xc0000005 &lt;/p&gt;    &lt;p&gt;Fault offset: 0x00027466 &lt;/p&gt;    &lt;p&gt;Faulting process id: 0xa38 &lt;/p&gt;    &lt;p&gt;Faulting application start time: 0x01cb6ef5e0cefe77 &lt;/p&gt;    &lt;p&gt;Faulting application path: C:\Program Files (x86)\Microsoft Office\Office14\OUTLOOK.EXE &lt;/p&gt;    &lt;p&gt;Faulting module path: C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE14\USP10.DLL&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;I read up online and found this article &lt;a title="http://social.technet.microsoft.com/Forums/en-US/outlook/thread/178bce4f-707e-42ac-8743-ac41b7a4e046/" href="http://social.technet.microsoft.com/Forums/en-US/outlook/thread/178bce4f-707e-42ac-8743-ac41b7a4e046/"&gt;http://social.technet.microsoft.com/Forums/en-US/outlook/thread/178bce4f-707e-42ac-8743-ac41b7a4e046/&lt;/a&gt; where a lot of people where having a similar problem.&lt;/p&gt;  &lt;p&gt;There were lot of opinions given there and they did nothing to help me fix the problem directly.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Microsoft&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I ended up opening a case with Microsoft and together over a period of a week we diagnosed the problem.&lt;/p&gt;  &lt;p&gt;The problem was – &lt;strong&gt;A Corrupt Font&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I found a few posts online which referred to Helvetica Font (above link and also other places) being corrupt and causing problems with Outlook. &lt;/p&gt;  &lt;p&gt;But in my case it was an &lt;strong&gt;Arial Narrow font.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;* I have suggested that Microsoft Write a tool to test all fonts for corruption – who knows what will happen.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;One of the responses from Microsoft was&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Based on my research I found that the affected emails which you sent to me is in Helvetica font. Looks like the issue is specific to Helvetica font.&lt;/p&gt;  &lt;p&gt;This problem may occur if your document contains Type 1 fonts, such as Helvetica and TimesNewRomanPS.    &lt;br /&gt;Note: The same problem can occur in Microsoft Office Outlook 2010 if an email message contains Type 1 fonts.&lt;/p&gt;  &lt;p&gt;To resolve this problem, please follow the action below.&lt;/p&gt;  &lt;p&gt;Determine which offending Type 1 font is being applied to your document, and then remove the offending Type 1 Font Substitutions entry from the registry. To do this, follow these steps: &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Click &lt;b&gt;Start&lt;/b&gt;, type &lt;b&gt;regedit&lt;/b&gt; in the &lt;strong&gt;Start Search&lt;/strong&gt; box, and then press Enter. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Collapse this imageExpand this image&lt;/p&gt;  &lt;p&gt;If you are prompted for an administrator password or for confirmation, type the password, or provide confirmation. &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;In Registry Editor, locate the following subkey: &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Under the &lt;strong&gt;FontSubstitutes&lt;/strong&gt; entry, select the offending Type 1 font. &lt;/li&gt;    &lt;li&gt;Right-click the font, and then click &lt;strong&gt;Delete&lt;/strong&gt;. &lt;/li&gt;    &lt;li&gt;Exit Registry Editor. &lt;/li&gt;    &lt;li&gt;Restart the computer. &lt;/li&gt;    &lt;li&gt;Uninstall the offending Type 1 fonts. For more information about how to uninstall fonts, see the following Microsoft Knowledge Base article: &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;a href="http://support.microsoft.com/kb/314960/"&gt;314960&lt;/a&gt; (http://support.microsoft.com/kb/314960/ ) How to install or remove a font in Windows&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Install the OpenType versions of the offending Type 1 fonts. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;For more detailed information, please refer to &lt;a href="http://support.microsoft.com/default.aspx?scid=kb;en-US;2119612"&gt;http://support.microsoft.com/default.aspx?scid=kb;en-US;2119612&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;** IMPORTANT ** The Microsoft response above did not fix the problem but more investigation showed that Arial fonts were the issue&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This problem was on a Windows 7 machine (and mine was as well) so I decided to copy the Arial fonts from the bad machine to my machine. After this “I” got the problem as well!!! (I made a backup of the fonts first!)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Fix (for me anyway) - Copy my good Arial Fonts to the bad computer&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In order to resolve this I needed to copy the good fonts from my machine to a temp directory on my machine and then copy them to a temp folder on the bad machine and then copy the font from the temp folder to the Fonts applet.&lt;/p&gt;  &lt;p&gt;Normally using the Fonts applet you can not copy fonts to anywhere else, but by opening a CMD prompt you can make copies of them.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Outlook2010crasheswhenopeningsomeemailme_98BE/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Outlook2010crasheswhenopeningsomeemailme_98BE/image_thumb.png" width="641" height="351" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So on the good computer open a CMD prompt and change directory to &lt;strong&gt;c:\windows\fonts&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;You can now copy the good fonts to another folder on the local machine such as c:\temp&lt;/p&gt;  &lt;p&gt;You now need to copy the fonts from the c:\temp folder to a folder on the bad computer – again c:\temp&lt;/p&gt;  &lt;p&gt;Then on the bad computer open the Fonts applet (you can just type &lt;strong&gt;fonts &lt;/strong&gt;and hit enter at the search programs and files area in the start menu)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Outlook2010crasheswhenopeningsomeemailme_98BE/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Outlook2010crasheswhenopeningsomeemailme_98BE/image_thumb_1.png" width="705" height="513" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now with that window open you can drag the fonts from c:\temp to the fonts window and you will be prompted to overwrite any existing fonts (if copying more than one font you can let it overwrite all fonts)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Outlook2010crasheswhenopeningsomeemailme_98BE/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Outlook2010crasheswhenopeningsomeemailme_98BE/image_thumb_2.png" width="370" height="165" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Some thoughts&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Now by the time I got around to checking these fonts we had sent 100’s of megabytes of debug logs to Microsoft, removed all add-ons, started in safe mode, rebooted heaps of times and generally spent a lot of time on the problem.&lt;/p&gt;  &lt;p&gt;I had been in contact with Microsoft on the phone and via email and a week or more time had passed with the user not being able to open emails from specific users.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;I would assume you could copy all fonts from a known good machine to the bad computer. &lt;/strong&gt;If you copy all fonts you may never know what font was corrupt, but there is a good chance that in less than 5 minutes you will have a working Outlook again.&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Also See&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Microsoft Office Word has encountered a problem and needs to close      &lt;br /&gt;&lt;a title="http://support.microsoft.com/default.aspx?scid=kb;en-US;2119612" href="http://support.microsoft.com/default.aspx?scid=kb;en-US;2119612"&gt;http://support.microsoft.com/default.aspx?scid=kb;en-US;2119612&lt;/a&gt;       &lt;br /&gt;      &lt;br /&gt;Microsoft Forum on this topic       &lt;br /&gt;&lt;a title="http://social.technet.microsoft.com/Forums/en-US/outlook/thread/178bce4f-707e-42ac-8743-ac41b7a4e046/" href="http://social.technet.microsoft.com/Forums/en-US/outlook/thread/178bce4f-707e-42ac-8743-ac41b7a4e046/"&gt;http://social.technet.microsoft.com/Forums/en-US/outlook/thread/178bce4f-707e-42ac-8743-ac41b7a4e046/&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blog.crowe.co.nz/aggbug/931.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Chris Crowe</dc:creator>
            <guid>http://blog.crowe.co.nz/archive/2010/11/03/Outlook-2010-crashes-when-opening-some-email-messages.aspx</guid>
            <pubDate>Tue, 02 Nov 2010 20:52:10 GMT</pubDate>
            <comments>http://blog.crowe.co.nz/archive/2010/11/03/Outlook-2010-crashes-when-opening-some-email-messages.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://blog.crowe.co.nz/comments/commentRss/931.aspx</wfw:commentRss>
        </item>
        <item>
            <title>The row value(s) updated or deleted either do not make the row unique or they alter multiple rows.</title>
            <link>http://blog.crowe.co.nz/archive/2010/08/30/The-row-values-updated-or-deleted-either-do-not-make.aspx</link>
            <description>&lt;p&gt;I had a user ask me today how they can delete a duplicate row from their database table. They were receiving the following error message when they tried to delete the duplicate row.&lt;/p&gt;  &lt;p&gt;&lt;i&gt;The row value&lt;/i&gt;(s) &lt;em&gt;updated or deleted either do not make the row unique or they alter multiple rows&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;The reason that the user gets this message is because the table did not have a Primary Key or other Unique Index defined. Why, that’s another story and not one I know!&lt;/p&gt;  &lt;p&gt;An example table&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Therowvaluesupdatedordeletedeitherdonot_B59F/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Therowvaluesupdatedordeletedeitherdonot_B59F/image_thumb.png" width="344" height="107" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Some example data&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Therowvaluesupdatedordeletedeitherdonot_B59F/image_4.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Therowvaluesupdatedordeletedeitherdonot_B59F/image_thumb_1.png" width="277" height="133" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Notice the duplicate row (row 1 and row 3)&lt;/p&gt;  &lt;p&gt;So the user in this case would select the first row and hit the delete button&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Therowvaluesupdatedordeletedeitherdonot_B59F/image_6.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Therowvaluesupdatedordeletedeitherdonot_B59F/image_thumb_2.png" width="431" height="165" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;They would then see this message&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Therowvaluesupdatedordeletedeitherdonot_B59F/image_8.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Therowvaluesupdatedordeletedeitherdonot_B59F/image_thumb_3.png" width="434" height="230" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So how do we get rid of the duplicate row?&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Two options&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Option #1&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Add a new column which contains unique values&lt;/p&gt;  &lt;p&gt;We could run the following code to add the new column and populate the new column with unique values&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;ALTER TABLE Table1       &lt;br /&gt;&lt;/strong&gt;&lt;strong&gt;ADD TempID int IDENTITY(1, 1)&lt;/strong&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;After issuing this command we will have something like this.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Therowvaluesupdatedordeletedeitherdonot_B59F/image_10.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/Therowvaluesupdatedordeletedeitherdonot_B59F/image_thumb_4.png" width="444" height="129" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The user can now select their row and hit delete because SQL Server can uniquely identify this row.&lt;/p&gt;  &lt;p&gt;You could then remove the new column&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;ALTER TABLE Table1       &lt;br /&gt;drop COLUMN TempID &lt;/strong&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;But this adds a new column to the table which is not really the best option so on to option #2&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Option #2&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;We can simply issue a simple delete command taking advantage of the &lt;strong&gt;Set RowCount &lt;/strong&gt;command.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;SET ROWCOUNT 1&lt;/strong&gt;&lt;/p&gt;   &lt;strong&gt;     &lt;p&gt;       &lt;br /&gt;&lt;/p&gt;     &lt;strong&gt;DELETE FROM Table1       &lt;br /&gt;&lt;/strong&gt;&lt;strong&gt;WHERE id= 1&lt;/strong&gt;&lt;/strong&gt;&lt;/blockquote&gt;  &lt;p&gt;What this does is limit the delete command to one record. Since the data is duplicated you do not care which row is deleted as long as only one of the rows is deleted. No adding and removing columns.&lt;/p&gt;&lt;img src="http://blog.crowe.co.nz/aggbug/929.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Chris Crowe</dc:creator>
            <guid>http://blog.crowe.co.nz/archive/2010/08/30/The-row-values-updated-or-deleted-either-do-not-make.aspx</guid>
            <pubDate>Mon, 30 Aug 2010 06:55:09 GMT</pubDate>
            <comments>http://blog.crowe.co.nz/archive/2010/08/30/The-row-values-updated-or-deleted-either-do-not-make.aspx#feedback</comments>
            <wfw:commentRss>http://blog.crowe.co.nz/comments/commentRss/929.aspx</wfw:commentRss>
        </item>
        <item>
            <title>WebMatrix - a complete Web development stack that integrates a Web server, Database and more</title>
            <link>http://blog.crowe.co.nz/archive/2010/07/06/WebMatrix---a-complete-Web-development-stack-that-integrates-a.aspx</link>
            <description>&lt;p&gt;WebMatrix includes a complete Web development stack that integrates a Web server (IIS Developer Express), database (Microsoft SQL Server Compact 4.0), programming model (ASP.NET Web pages with Razor syntax), and a tool (WebMatrix Beta) into a seamless experience.  You can use WebMatrix to streamline the way you create an ASP.NET Web site from templates, or by starting a new Web site by using the world’s most popular free and open source (ASP.NET or PHP) apps like DotNetNuke, Umbraco, WordPress, or Joomla!. With WebMatrix you can code your Web sites, customize them, optimize them for good search engine ranking, test them, and deploy them to an Internet hosting company, all through the tool.&lt;/p&gt;  &lt;p&gt;Learn more about WebMatrix through:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkID=195076&amp;amp;clcid=0x409"&gt;What is WebMatrix&lt;/a&gt;?  &lt;/li&gt;    &lt;li&gt;Investigate the top &lt;a href="http://go.microsoft.com/fwlink/?LinkID=195935&amp;amp;clcid=0x409"&gt;10 features&lt;/a&gt; of WebMatrix.&lt;/li&gt;    &lt;li&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkID=195938&amp;amp;clcid=0x409"&gt;Download WebMatrix Beta&lt;/a&gt;.&lt;/li&gt;    &lt;li&gt;More about &lt;a href="http://go.microsoft.com/fwlink/?LinkID=195075&amp;amp;clcid=0x409"&gt;IIS Developer Express&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;More about &lt;a href="http://go.microsoft.com/fwlink/?LinkID=195939&amp;amp;clcid=0x409"&gt;Microsoft SQL Server Compact 4.0&lt;/a&gt;.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;For additional resources, visit:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkID=196364&amp;amp;clcid=0x409"&gt;Learn more&lt;/a&gt; about WebMatrix Beta. More resources, links to forums and feedback channels.&lt;/li&gt;    &lt;li&gt;Visit the forums on the &lt;a href="http://go.microsoft.com/fwlink/?LinkID=195074&amp;amp;clcid=0x409"&gt;IIS Web site&lt;/a&gt; or the &lt;a href="http://go.microsoft.com/fwlink/?LinkID=193026&amp;amp;clcid=0x409"&gt;ASP.NET Web site&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkID=195940&amp;amp;clcid=0x409"&gt;File a bug or suggestion&lt;/a&gt; on WebMatrix Beta.&lt;/li&gt;    &lt;li&gt;Learn to program the Web using &lt;a href="http://go.microsoft.com/fwlink/?LinkID=195072&amp;amp;clcid=0x409"&gt;ASP.NET Web pages with Razor syntax&lt;/a&gt;.&lt;/li&gt;    &lt;li&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkID=195073&amp;amp;clcid=0x409"&gt;Download applications with WebMatrix&lt;/a&gt; and the Web Application Gallery.&lt;/li&gt;    &lt;li&gt;Review the &lt;a href="http://go.microsoft.com/fwlink/?LinkID=196179&amp;amp;clcid=0x409"&gt;FAQs&lt;/a&gt; for Open Source and Free Web applications.&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blog.crowe.co.nz/aggbug/928.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Chris Crowe</dc:creator>
            <guid>http://blog.crowe.co.nz/archive/2010/07/06/WebMatrix---a-complete-Web-development-stack-that-integrates-a.aspx</guid>
            <pubDate>Tue, 06 Jul 2010 01:49:26 GMT</pubDate>
            <comments>http://blog.crowe.co.nz/archive/2010/07/06/WebMatrix---a-complete-Web-development-stack-that-integrates-a.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://blog.crowe.co.nz/comments/commentRss/928.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Looking for a way to find text in your SQL Database objects, then try the free Re-Gate SQL Search</title>
            <link>http://blog.crowe.co.nz/archive/2010/06/27/Looking-for-a-way-to-find-text-in-your-SQL.aspx</link>
            <description>&lt;p&gt;To start I should mention that you can not search the actual data in your tables for text, but you can search your SQL Objects – Tables, Views, Stored Procedures, Constraints, Triggers and Functions for text.&lt;/p&gt;  &lt;p&gt;Often you want to find all references to a particular piece of text which could be the name of a table or column or just something that you have in a stored procedure.&lt;/p&gt;  &lt;p&gt;The free tool from Red-Gate software fits the bill well, supporting SQL 2005 and above (including the express editions of Management Studio – See below for notes on SQL Express 2005)&lt;/p&gt;  &lt;p&gt;There is some advertising links to their other products included in the tool, but they are well worth a look at if you have never used them before – Red-Gate products are great.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Downloading the tool&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;To download the tool simply go to &lt;a title="http://www.red-gate.com/products/sql_search/" href="http://www.red-gate.com/products/sql_search/"&gt;http://www.red-gate.com/products/sql_search/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Installation     &lt;br /&gt;&lt;/strong&gt;After you install SQL Search you get a new toolbar with a single button &lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/LookingforawaytofindtextinyourSQLDatabas_CD32/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/LookingforawaytofindtextinyourSQLDatabas_CD32/image_thumb.png" width="111" height="33" /&gt;&lt;/a&gt;  in SQL Server Management Studio. Clicking this button opens the UI which add a new tab to the current set of open tabs and will select the current database as the database to be searched (you can change this from the drop down). &lt;/p&gt;  &lt;p&gt;Note: You can only search a single database at a time.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/LookingforawaytofindtextinyourSQLDatabas_CD32/image_4.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/LookingforawaytofindtextinyourSQLDatabas_CD32/image_thumb_1.png" width="800" height="68" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The search is activated by simply typing into the search field and your results are immediately displayed in a grid (no need to hit enter). &lt;/p&gt;  &lt;p&gt;You can limit the search to different types of objects such as tables or views if needed.&lt;/p&gt;  &lt;p&gt;Clicking on any of the search results will display the full text of the object where it was found with any matching text highlighted.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/LookingforawaytofindtextinyourSQLDatabas_CD32/image_6.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/LookingforawaytofindtextinyourSQLDatabas_CD32/image_thumb_2.png" width="800" height="367" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;SQL 2005 Management Studio Express&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This statement is included on the requirements page for SQL Search at &lt;a title="http://www.red-gate.com/products/sql_search/requirements.htm" href="http://www.red-gate.com/products/sql_search/requirements.htm"&gt;http://www.red-gate.com/products/sql_search/requirements.htm&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;* To use SQL Search with SSMS 2005 Express Edition, &lt;em&gt;extensibility.dll&lt;/em&gt; must be installed.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;But then they give you no indication of what that means or where you get it from. &lt;/p&gt;  &lt;p&gt;You can download the &lt;b&gt;extensibility&lt;/b&gt;.&lt;b&gt;dll&lt;/b&gt; by installing the following tool:    &lt;br /&gt;&lt;a href="ftp://ftp.red-gate.com/development/sqlprompt/extensibilitymsm.msi"&gt;ftp://ftp.red-gate.com/development/sqlprompt/extensibilitymsm.msi&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blog.crowe.co.nz/aggbug/927.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Chris Crowe</dc:creator>
            <guid>http://blog.crowe.co.nz/archive/2010/06/27/Looking-for-a-way-to-find-text-in-your-SQL.aspx</guid>
            <pubDate>Sun, 27 Jun 2010 08:35:47 GMT</pubDate>
            <comments>http://blog.crowe.co.nz/archive/2010/06/27/Looking-for-a-way-to-find-text-in-your-SQL.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://blog.crowe.co.nz/comments/commentRss/927.aspx</wfw:commentRss>
        </item>
        <item>
            <title>SQL Server : TSQL Return start/end price and start/end date time by product name</title>
            <link>http://blog.crowe.co.nz/archive/2010/06/22/SQL-Server--TSQL-Return-startend-price-and-startend-date.aspx</link>
            <description>&lt;p&gt;As part of trying to monitor my power usage and to know when my power prices change I download the power prices online from my power provider every 15 minutes and store this in a database.&lt;/p&gt;  &lt;p&gt;I have been doing this for more than 4 months now and wanted to produce some data showing how the prices have changed over time.&lt;/p&gt;  &lt;p&gt;I wanted to produce a report of all of the price movements per product over time.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_8.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_thumb_3.png" width="535" height="212" /&gt;&lt;/a&gt;     &lt;br /&gt;&lt;/p&gt;  &lt;h3&gt;The Database&lt;/h3&gt;  &lt;p&gt;&lt;strong&gt;I have two SQL Server tables described below that contain the data: &lt;/strong&gt;&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;TABLE&lt;/span&gt; [dbo].[PowerUpdate](
    [id] [&lt;span class="kwrd"&gt;int&lt;/span&gt;] &lt;span class="kwrd"&gt;IDENTITY&lt;/span&gt;(1,1) &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [DateTime] [datetime] &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [Enabled] [&lt;span class="kwrd"&gt;bit&lt;/span&gt;] &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;
)&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;TABLE&lt;/span&gt; [dbo].[PowerItem](
    [id] [&lt;span class="kwrd"&gt;int&lt;/span&gt;] &lt;span class="kwrd"&gt;IDENTITY&lt;/span&gt;(1,1) &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [PowerUpdateID] [&lt;span class="kwrd"&gt;int&lt;/span&gt;] &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [Name] [nvarchar](128) &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [Price] [&lt;span class="kwrd"&gt;decimal&lt;/span&gt;](9, 4) &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [Type] [nvarchar](128) &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [Description] [ntext] &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
)&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;I also have one view that just joins the tables back together for reporting purposes.&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;VIEW&lt;/span&gt; [dbo].[vPowerData]
&lt;span class="kwrd"&gt;as&lt;/span&gt;
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt;     PowerItem_1.id, PowerItem_1.Name, PowerItem_1.Price, PowerUpdate_1.DateTime
&lt;span class="kwrd"&gt;FROM&lt;/span&gt;       PowerItem &lt;span class="kwrd"&gt;AS&lt;/span&gt; PowerItem_1 &lt;br /&gt;&lt;span class="kwrd"&gt;INNER&lt;/span&gt; &lt;span class="kwrd"&gt;JOIN &lt;/span&gt;PowerUpdate &lt;span class="kwrd"&gt;AS&lt;/span&gt; PowerUpdate_1 &lt;span class="kwrd"&gt;ON&lt;/span&gt; PowerItem_1.PowerUpdateID = PowerUpdate_1.id&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[










.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;/p&gt;

&lt;h3&gt;Some sample data&lt;/h3&gt;

&lt;p&gt;So here is some examples of the data that I have in the database. &lt;/p&gt;

&lt;table border="0" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top"&gt;&lt;strong&gt;select top 10 * from powerupdate&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top"&gt;&lt;strong&gt;select top 10 * from poweritem&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top"&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_thumb.png" width="252" height="212" /&gt;&lt;/a&gt;&lt;/td&gt;

      &lt;td valign="top"&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_thumb_2.png" width="673" height="211" /&gt;&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;PowerUpdate&lt;/strong&gt; contains records with an ID and a Date and Time which identifies when the update occurred.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PowerItem&lt;/strong&gt; contains the actual power products that were available for the particular update date &amp;amp; time and their current price.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;What I want in my report&lt;/h3&gt;

&lt;p&gt;What I would like to see is for every product, the date and time when the price was first seen, and the last date and time that the price was seen and the actual prices.&lt;/p&gt;

&lt;p&gt;To make it more complex a product’s price could return to the same value as a previous date and time and I would like the last price to display null for the next price and next price datetime fields.&lt;/p&gt;

&lt;p&gt;Here is an example of the required output 
  &lt;br /&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_8.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_thumb_3.png" width="535" height="212" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;The final solution&lt;/h3&gt;

&lt;p&gt;Now I had problems trying to get my head around my report requirement. I spent some time trying to come up with a solution but failed – I knew that it could be done using a SET based solution, which is what I wanted, but I could not do it myself.&lt;/p&gt;

&lt;p&gt;I posted my question to the guru’s at &lt;a href="http://www.sqlservercentral.com"&gt;www.sqlservercentral.com&lt;/a&gt; – the original post is here &lt;a title="http://www.sqlservercentral.com/Forums/Topic930464-338-1.aspx" href="http://www.sqlservercentral.com/Forums/Topic930464-338-1.aspx"&gt;http://www.sqlservercentral.com/Forums/Topic930464-338-1.aspx&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I received some good replies from people on the forum and really appreciate the work all the people put into this, to help me!&lt;/p&gt;

&lt;p&gt;I liked the solution provided by a member called “&lt;a href="http://www.sqlservercentral.com/Forums/UserInfo106082.aspx"&gt;Mark-101232&lt;/a&gt;” &lt;/p&gt;

&lt;p&gt;Below is his solution to the problem which did exactly what I needed and was very fast compared to some solutions given.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;WITH&lt;/span&gt; CTE1 &lt;span class="kwrd"&gt;AS&lt;/span&gt; (
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; Name ,Price ,DateTime,
       ROW_NUMBER() &lt;span class="kwrd"&gt;OVER&lt;/span&gt;(PARTITION &lt;span class="kwrd"&gt;BY&lt;/span&gt; Name &lt;span class="kwrd"&gt;ORDER&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; DATETime) &lt;span class="kwrd"&gt;AS&lt;/span&gt; rn1,
       ROW_NUMBER() &lt;span class="kwrd"&gt;OVER&lt;/span&gt;(PARTITION &lt;span class="kwrd"&gt;BY&lt;/span&gt; Name,Price &lt;span class="kwrd"&gt;ORDER&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; DATETime) &lt;span class="kwrd"&gt;AS&lt;/span&gt; rn2
&lt;span class="kwrd"&gt;FROM&lt;/span&gt; dbo.vPowerData),
CTE2 &lt;span class="kwrd"&gt;AS&lt;/span&gt; (
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; Name,Price &lt;span class="kwrd"&gt;AS&lt;/span&gt; [&lt;span class="kwrd"&gt;Min&lt;/span&gt; Price],&lt;span class="kwrd"&gt;MIN&lt;/span&gt;(DateTime) &lt;span class="kwrd"&gt;AS&lt;/span&gt; [&lt;span class="kwrd"&gt;Min&lt;/span&gt; DateTime],&lt;span class="kwrd"&gt;MAX&lt;/span&gt;(rn1) &lt;span class="kwrd"&gt;AS&lt;/span&gt; maxRN
&lt;span class="kwrd"&gt;FROM&lt;/span&gt; CTE1
&lt;span class="kwrd"&gt;GROUP&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; Name,Price,rn2-rn1)
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; a.Name,a.[&lt;span class="kwrd"&gt;Min&lt;/span&gt; Price],a.[&lt;span class="kwrd"&gt;Min&lt;/span&gt; DateTime],
       b.Price &lt;span class="kwrd"&gt;AS&lt;/span&gt; [NextPrice],
       b.DateTime &lt;span class="kwrd"&gt;AS&lt;/span&gt; [&lt;span class="kwrd"&gt;Next&lt;/span&gt; Price DateTime]
&lt;span class="kwrd"&gt;FROM&lt;/span&gt; CTE2 a
&lt;span class="kwrd"&gt;LEFT&lt;/span&gt; &lt;span class="kwrd"&gt;OUTER&lt;/span&gt; &lt;span class="kwrd"&gt;JOIN&lt;/span&gt; CTE1 b &lt;span class="kwrd"&gt;ON&lt;/span&gt; b.Name=a.Name &lt;span class="kwrd"&gt;AND&lt;/span&gt; b.rn1=a.maxRN+1 &lt;span class="kwrd"&gt;AND&lt;/span&gt; b.DateTime&amp;gt;a.[&lt;span class="kwrd"&gt;Min&lt;/span&gt; DateTime]
&lt;span class="kwrd"&gt;ORDER&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; a.Name,a.[&lt;span class="kwrd"&gt;Min&lt;/span&gt; DateTime];

&lt;hr /&gt;&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Now although I have a solution I really need to understand the logic here so I decided to write this blog post to try to strip down this TSQL code so I can actually understand what is going on and in the process this may help someone else as well.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;/p&gt;

&lt;h3&gt;The first step is to split the query down into its parts.&lt;/h3&gt;

&lt;h3&gt;&lt;strong&gt;Part 1&lt;/strong&gt;&lt;/h3&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; Name ,Price ,DateTime,
       ROW_NUMBER() &lt;span class="kwrd"&gt;OVER&lt;/span&gt;(PARTITION &lt;span class="kwrd"&gt;BY&lt;/span&gt; Name &lt;span class="kwrd"&gt;ORDER&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; DATETime) &lt;span class="kwrd"&gt;AS&lt;/span&gt; rn1,
       ROW_NUMBER() &lt;span class="kwrd"&gt;OVER&lt;/span&gt;(PARTITION &lt;span class="kwrd"&gt;BY&lt;/span&gt; Name,Price &lt;span class="kwrd"&gt;ORDER&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; DATETime) &lt;span class="kwrd"&gt;AS&lt;/span&gt; rn2
&lt;span class="kwrd"&gt;FROM&lt;/span&gt; dbo.vPowerData&lt;/pre&gt;

&lt;pre class="csharpcode"&gt; &lt;/pre&gt;

&lt;p&gt;What this is TSQL is doing is return the name, price and date time and also two row numbers. &lt;/p&gt;

&lt;p&gt;For more details on row_number() see &lt;a title="http://msdn.microsoft.com/en-us/library/ms186734.aspx" href="http://msdn.microsoft.com/en-us/library/ms186734.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms186734.aspx&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first row_number() function returns the a row number (resetting it to 1 when the name changes) and ordering it by the date time&lt;/p&gt;

&lt;p&gt;The second row_number() function returns the a row number (resetting it to 1 when the name and price changes) and ordering it by the date time&lt;/p&gt;

&lt;p&gt;The easiest way to look at this is to look at a subset of data and show you what gets returned.&lt;/p&gt;

&lt;p&gt;Assume our PowerItem table only contains the following records&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_18.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_thumb_8.png" width="315" height="251" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: There are 3 different products listed, but only the “$49.95 Value Pack” has had the price changing as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;0.1693 &lt;/li&gt;

  &lt;li&gt;0.1698 &lt;/li&gt;

  &lt;li&gt;0.1691 &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The TSQL code in &lt;strong&gt;Part 1&lt;/strong&gt; will return the following result set &lt;/p&gt;

&lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_20.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_thumb_9.png" width="388" height="249" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;Part 2&lt;/h3&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; Name,Price &lt;span class="kwrd"&gt;AS&lt;/span&gt; [&lt;span class="kwrd"&gt;Min&lt;/span&gt; Price],&lt;span class="kwrd"&gt;MIN&lt;/span&gt;(DateTime) &lt;span class="kwrd"&gt;AS&lt;/span&gt; [&lt;span class="kwrd"&gt;Min&lt;/span&gt; DateTime],&lt;span class="kwrd"&gt;MAX&lt;/span&gt;(rn1) &lt;span class="kwrd"&gt;AS&lt;/span&gt; maxRN
&lt;span class="kwrd"&gt;FROM&lt;/span&gt; CTE1
&lt;span class="kwrd"&gt;GROUP&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; Name,Price,rn2-rn1&lt;/pre&gt;

&lt;p&gt;Note: In this case treat CTE1 as the final result set that is visible in Part 1 above&lt;/p&gt;

&lt;p&gt;The TSQL code in &lt;strong&gt;Part 2&lt;/strong&gt; returns the following result set&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_24.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_thumb_11.png" width="390" height="115" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;Part 3&lt;/h3&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; a.Name,a.[&lt;span class="kwrd"&gt;Min&lt;/span&gt; Price],a.[&lt;span class="kwrd"&gt;Min&lt;/span&gt; DateTime],
       b.Price &lt;span class="kwrd"&gt;AS&lt;/span&gt; [NextPrice],
       b.DateTime &lt;span class="kwrd"&gt;AS&lt;/span&gt; [&lt;span class="kwrd"&gt;Next&lt;/span&gt; Price DateTime]
&lt;span class="kwrd"&gt;FROM&lt;/span&gt; CTE2 a
&lt;span class="kwrd"&gt;LEFT&lt;/span&gt; &lt;span class="kwrd"&gt;OUTER&lt;/span&gt; &lt;span class="kwrd"&gt;JOIN&lt;/span&gt; CTE1 b &lt;span class="kwrd"&gt;ON&lt;/span&gt; b.Name=a.Name &lt;span class="kwrd"&gt;AND&lt;/span&gt; b.rn1=a.maxRN+1 &lt;span class="kwrd"&gt;AND&lt;/span&gt; b.DateTime&amp;gt;a.[&lt;span class="kwrd"&gt;Min&lt;/span&gt; DateTime]
&lt;span class="kwrd"&gt;ORDER&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; a.Name,a.[&lt;span class="kwrd"&gt;Min&lt;/span&gt; DateTime];&lt;/pre&gt;

&lt;p&gt;Part 3 is really just a simple Left Outer Join sorted by name and the Minimum Date Time, the reason for the left outer join is that we want the final price and date.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hint&lt;/strong&gt;: I often try to think as CTE (Common Table Expressions) as a physical table and you can therefore logically join it easier (well it works for me - sometimes)&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;table border="0" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top"&gt;&lt;strong&gt;CTE 1 AS B&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top"&gt;&lt;strong&gt;CTE 2 AS A&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top"&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_22.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_thumb_10.png" width="388" height="249" /&gt;&lt;/a&gt; &lt;/td&gt;

      &lt;td valign="top"&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_26.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_thumb_12.png" width="390" height="115" /&gt;&lt;/a&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;So in this case we have the following going on (for the product name ‘$49.95 Value Pack’)&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Row 1 
    &lt;ul&gt;
      &lt;li&gt;Row 1 does not join to CTE2 since b.rn1 (in this case 4) = a.maxRN+1 (in this case 5) does not exist &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Row 2 
    &lt;ul&gt;
      &lt;li&gt;Row 2 does not join to CTE2 since b.rn1 (in this case 1) = a.maxRN+1 (in this case 2) does not exist &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Row 3 
    &lt;ul&gt;
      &lt;li&gt;Row 3 does join CTE2 since b.rn1 (in this case 2) = a.maxRN+1 (in this case 3) does exist &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Row 4 
    &lt;ul&gt;
      &lt;li&gt;Row 4 does join CTE2 since b.rn1 (in this case 3) = a.maxRN+1 (in this case 4) does exist &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: there is also other filtered here such as b.name = a.name and b.datetime &amp;gt; a.[min datetime]&lt;/p&gt;

&lt;p&gt;So for the first 4 rows this products the following output (when the order by clause is added)&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_28.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.crowe.co.nz/blog/images/blog_crowe_co_nz/blog/WindowsLiveWriter/SQLServerCTEtoreturn_BA46/image_thumb_13.png" width="538" height="79" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Now I hope I have explained what is going on because I am still trying to decipher and understand this myself.&lt;/p&gt;

&lt;p&gt;But in any case it has made it easier for me to comprehend so I am happy with that and if it helps anyone else then that is good.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Additional resources to create database and populate with data&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://blog.crowe.co.nz/attachments/CreatePowerShopDatabaseAndTable.txt"&gt;Script to create database and tables&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://blog.crowe.co.nz/attachments/powerdata.rar"&gt;Script to add 4 months worth of data&lt;/a&gt; (1 MB download, expands to 150MB)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Additional indexes could help in the solution as well. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Additional comments:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Many thanks to those that answered or submitted a potential solution to my query on SQL Server Central – it was very much appreciated. I am in no way trying to take away from what you have done, but instead hope I am helping others in the SQL Server community by understanding the particular solution I choose to accept, and hopefully explaining it so we can all learn and be better TSQL developers. &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blog.crowe.co.nz/aggbug/926.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Chris Crowe</dc:creator>
            <guid>http://blog.crowe.co.nz/archive/2010/06/22/SQL-Server--TSQL-Return-startend-price-and-startend-date.aspx</guid>
            <pubDate>Tue, 22 Jun 2010 10:14:37 GMT</pubDate>
            <comments>http://blog.crowe.co.nz/archive/2010/06/22/SQL-Server--TSQL-Return-startend-price-and-startend-date.aspx#feedback</comments>
            <wfw:commentRss>http://blog.crowe.co.nz/comments/commentRss/926.aspx</wfw:commentRss>
        </item>
        <item>
            <title>An error occurred in the Microsoft .NET Framework while trying to load assembly id 65551. The server may be running out of resources, or the assembly may not be trusted with PERMISSION_SET = EXTERNAL_ACCESS or UNSAFE</title>
            <link>http://blog.crowe.co.nz/archive/2010/06/04/An-error-occurred-in-the-Microsoft-.NET-Framework-while-trying.aspx</link>
            <description>&lt;h3&gt;Server Error in '/' Application.    &lt;hr size="1" width="100%" /&gt;&lt;/h3&gt;  &lt;h4&gt;&lt;i&gt;An error occurred in the Microsoft .NET Framework while trying to load assembly id 65551. The server may be running out of resources, or the assembly may not be trusted with PERMISSION_SET = EXTERNAL_ACCESS or UNSAFE. Run the query again, or check documentation to see how to solve the assembly trust issues. For more information about this error:      &lt;br /&gt;System.IO.FileLoadException: Could not load file or assembly 'sqlservertimezones, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f34ea75f04c42955' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)      &lt;br /&gt;System.IO.FileLoadException:       &lt;br /&gt;  at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark&amp;amp; stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)      &lt;br /&gt;  at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark&amp;amp; stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)      &lt;br /&gt;  at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark&amp;amp; stackMark, Boolean forIntrospection)      &lt;br /&gt;  at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark&amp;amp; stackMark, Boolean forIntrospection)      &lt;br /&gt;  at System.Reflection.Assembly.Load(String assemblyString)      &lt;br /&gt;&lt;/i&gt;&lt;/h4&gt;  &lt;p&gt;I recently got this error after moving a database from SQL Server 2008 to SQL Server 2008 R2 – there are some articles that can help you with this error:&lt;/p&gt;  &lt;p&gt;Msg 10314, Level 16, State 11, Line 1&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;a title="http://support.microsoft.com/kb/918040" href="http://support.microsoft.com/kb/918040"&gt;http://support.microsoft.com/kb/918040&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a title="http://blogs.msdn.com/b/psssql/archive/2010/03/23/unable-to-load-clr-assembly-intermittently.aspx" href="http://blogs.msdn.com/b/psssql/archive/2010/03/23/unable-to-load-clr-assembly-intermittently.aspx"&gt;http://blogs.msdn.com/b/psssql/archive/2010/03/23/unable-to-load-clr-assembly-intermittently.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a title="http://www.sqlnewsgroups.net/group/microsoft.public.sqlserver.server/topic16037.aspx" href="http://www.sqlnewsgroups.net/group/microsoft.public.sqlserver.server/topic16037.aspx"&gt;http://www.sqlnewsgroups.net/group/microsoft.public.sqlserver.server/topic16037.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;In my case none of the solutions helped me and then I thought about the actual assembly. My SQL Server 2008 is running on Windows Server 2008 64Bit, but the SQL Server 2008 R2 was running on 32Bit Windows Server 2008.&lt;/p&gt;  &lt;p&gt;I had a look at the assembly compilation properties in Visual studio and noticed that it was a 64Bit DLL and not a 32bit one.&lt;/p&gt;  &lt;p&gt;I recompiled and replaced the DLL and reran my setup for the assembly and it resolved my problem.&lt;/p&gt;&lt;img src="http://blog.crowe.co.nz/aggbug/924.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Chris Crowe</dc:creator>
            <guid>http://blog.crowe.co.nz/archive/2010/06/04/An-error-occurred-in-the-Microsoft-.NET-Framework-while-trying.aspx</guid>
            <pubDate>Fri, 04 Jun 2010 07:28:24 GMT</pubDate>
            <comments>http://blog.crowe.co.nz/archive/2010/06/04/An-error-occurred-in-the-Microsoft-.NET-Framework-while-trying.aspx#feedback</comments>
            <wfw:commentRss>http://blog.crowe.co.nz/comments/commentRss/924.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Looking for some new sounds &amp;ndash; checkout this freebee</title>
            <link>http://blog.crowe.co.nz/archive/2009/07/18/Looking-for-some-new-sounds-ndash-checkout-this-freebee.aspx</link>
            <description>&lt;p&gt;Click to download new music, completely free, brought to you by your music loving friends at Windows. You'll see (but not hear) some ads on your screen in addition to the album's cover art, which is how we (they) keep your new tunes from costing you a cent.&lt;/p&gt;  &lt;p&gt;Well worth a listen to checkout some new bands and artists….&lt;/p&gt;  &lt;p&gt;Listen online, download MP3 or WMA&lt;/p&gt;  &lt;p&gt;&lt;a title="http://sponsoredsongs.reverbnation.com/windows?utm_source=windows_myspace_page&amp;amp;utm_medium=button_link&amp;amp;utm_campaign=ms_windows&amp;amp;utm_content=thousand_more" href="http://sponsoredsongs.reverbnation.com/windows?utm_source=windows_myspace_page&amp;amp;utm_medium=button_link&amp;amp;utm_campaign=ms_windows&amp;amp;utm_content=thousand_more"&gt;http://sponsoredsongs.reverbnation.com/windows?utm_source=windows_myspace_page&amp;amp;utm_medium=button_link&amp;amp;utm_campaign=ms_windows&amp;amp;utm_content=thousand_more&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blog.crowe.co.nz/aggbug/923.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Chris Crowe</dc:creator>
            <guid>http://blog.crowe.co.nz/archive/2009/07/18/Looking-for-some-new-sounds-ndash-checkout-this-freebee.aspx</guid>
            <pubDate>Fri, 17 Jul 2009 20:40:37 GMT</pubDate>
            <comments>http://blog.crowe.co.nz/archive/2009/07/18/Looking-for-some-new-sounds-ndash-checkout-this-freebee.aspx#feedback</comments>
            <wfw:commentRss>http://blog.crowe.co.nz/comments/commentRss/923.aspx</wfw:commentRss>
        </item>
        <item>
            <title>An great TSQL index defrag script</title>
            <link>http://blog.crowe.co.nz/archive/2009/07/15/An-great-TSQL-index-defrag-script.aspx</link>
            <description>&lt;p&gt;I have been doing some more looking at SQL Server indexes lately and came across a great script that may be of use to others as well.&lt;/p&gt;  &lt;p&gt;So what can this index defrag script do? &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Schedule it to run with the default settings; it works “right out of the box” with no additional configuration necessary &lt;/li&gt;    &lt;li&gt;Run this one script from a centralized database for all databases on a server &lt;/li&gt;    &lt;li&gt;Run this script for a specific database or table &lt;/li&gt;    &lt;li&gt;Configure custom threshold limits and the point at which a rebuild should be performed (instead of a reorganize) &lt;/li&gt;    &lt;li&gt;Defrag individual partitions &lt;/li&gt;    &lt;li&gt;Log its actions and the duration of the defrag &lt;/li&gt;    &lt;li&gt;Run in “commands only” mode (@executeSQL = 0, @printCommands = 1) &lt;/li&gt;    &lt;li&gt;Customize performance parameters such as @maxDopRestriction and @defragDelay to minimize impact on the server &lt;/li&gt;    &lt;li&gt;Schedule specific indexes to only be defragged on weekends, or every other day&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The script source code (very well documented) and author (&lt;strong&gt;Michelle Ufford&lt;/strong&gt;) can be found at &lt;a title="http://sqlfool.com/2009/06/index-defrag-script-v30/" href="http://sqlfool.com/2009/06/index-defrag-script-v30/"&gt;http://sqlfool.com/2009/06/index-defrag-script-v30/&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blog.crowe.co.nz/aggbug/922.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Chris Crowe</dc:creator>
            <guid>http://blog.crowe.co.nz/archive/2009/07/15/An-great-TSQL-index-defrag-script.aspx</guid>
            <pubDate>Tue, 14 Jul 2009 19:39:16 GMT</pubDate>
            <comments>http://blog.crowe.co.nz/archive/2009/07/15/An-great-TSQL-index-defrag-script.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blog.crowe.co.nz/comments/commentRss/922.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Some simple T-SQL code to strip time from a date and return other dates</title>
            <link>http://blog.crowe.co.nz/archive/2009/07/14/Some-simple-T-SQL-code-to-strip-time-from-a-date.aspx</link>
            <description>&lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;p&gt;&lt;font size="2"&gt;&lt;span style="color: #0000ff"&gt;declare&lt;/span&gt; @dt datetime&lt;br /&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt; @dt = getdate()&lt;/font&gt;&lt;font size="2"&gt;&lt;br /&gt; &lt;/font&gt;&lt;/p&gt;&lt;span style="color: #0000ff"&gt;&lt;/span&gt;&lt;p&gt;&lt;font size="2"&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;'Now'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; @dt&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;''&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;&lt;span style="color: #006080"&gt;&lt;/span&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;'Today - no time'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; dateadd(D, 0, datediff(D, 0, @dt))&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;''&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;'Today – 23:59:59.997'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; dateadd(ms,-3, dateadd(D, datediff(D, 0, @dt),1))&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;''&lt;/span&gt;&lt;span style="color: #006080"&gt;&lt;/span&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;'Start of this week - Starts on MONDAY'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; dateadd(wk,datediff(wk,0, @dt),0) &lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;''&lt;/span&gt;&lt;/p&gt;&lt;span style="color: #006080"&gt;&lt;/span&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;'End of this week - Starts on MONDAY 23:59:59.997'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; dateadd(ms,-3, dateadd(wk,datediff(wk,0,@dt),7))&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;''&lt;/span&gt;&lt;/p&gt;&lt;span style="color: #006080"&gt;&lt;/span&gt;&lt;p&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;'Start of this week - Starts on SUNDAY'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; dateadd(wk,datediff(wk,0,getdate()),-1) &lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;''&lt;/span&gt;&lt;/p&gt;&lt;span style="color: #006080"&gt;&lt;/span&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;'End of this week - Starts on SUNDAY 23:59:59.997'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; dateadd(ms,-3,dateadd(wk,datediff(wk,0,@dt),5)) &lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;''&lt;/span&gt;&lt;/p&gt;&lt;span style="color: #006080"&gt;&lt;/span&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;'Start of Month'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; dateadd(mm,datediff(mm,0,@dt),0) &lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;''&lt;/span&gt;&lt;/p&gt;&lt;span style="color: #006080"&gt;&lt;/span&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;'Last day of current Month - 23:59:59.997'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; dateadd(ms,-3, dateadd(mm,datediff(mm,0,@dt)+1,0))&lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;''&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #006080"&gt;&lt;/span&gt;&lt;/p&gt;&lt;/font&gt; &lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Here is a simple break down to how these snippets actually work&lt;/p&gt;

&lt;p&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;'Today - no time'&lt;/span&gt;

  &lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; dateadd(D, 0, datediff(D, 0, @dt))

  &lt;br /&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; &lt;span style="color: #006080"&gt;''&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;First off we return the &lt;strong&gt;&lt;em&gt;number of days&lt;/em&gt;&lt;/strong&gt; between the date/time passed and &lt;strong&gt;0&lt;/strong&gt; so we get the number of whole days (we have lost the time)&lt;/p&gt;

&lt;p&gt;Today is July 14, 2009 (as an example) which using this code&lt;/p&gt;

&lt;p&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; datediff(D, 0, '2009-07-14 03:34:21')&lt;/p&gt;

&lt;p&gt;This will return &lt;strong&gt;40006 &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We then add &lt;strong&gt;40006 &lt;/strong&gt;days to &lt;strong&gt;0&lt;/strong&gt; and this returns the date &lt;strong&gt;2009-07-14&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; dateadd(D, 0, 40006)&lt;/p&gt;

&lt;p&gt;Jul 14 2009 12:00AM&lt;/p&gt;

&lt;p&gt;So adding it together we get&lt;/p&gt;

&lt;p&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt; dateadd(D, 0, datediff(D, 0, @dt))&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;You will notice that some of the functions have a &lt;strong&gt;–3&lt;/strong&gt; as part of the code, this is because we want to return the last actual time that SQL Server can reference on a particular day. SQL Server can store the maximum time as &lt;strong&gt;23:59:59.997&lt;/strong&gt;. That is because SQL Server is only accurate to 3 milliseconds. If we used 23:59:59.999 it would actually roll over to the next day.&lt;/p&gt;

&lt;p&gt;So if we were wanting to return the last time for today’s date/time we can use the following&lt;/p&gt;

&lt;p&gt;print DATEADD(ms, - 3, DATEADD(D, DATEDIFF(D, 0, GETDATE()), 1)) 
  &lt;/p&gt;&lt;img src="http://blog.crowe.co.nz/aggbug/921.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Chris Crowe</dc:creator>
            <guid>http://blog.crowe.co.nz/archive/2009/07/14/Some-simple-T-SQL-code-to-strip-time-from-a-date.aspx</guid>
            <pubDate>Mon, 13 Jul 2009 22:55:04 GMT</pubDate>
            <comments>http://blog.crowe.co.nz/archive/2009/07/14/Some-simple-T-SQL-code-to-strip-time-from-a-date.aspx#feedback</comments>
            <wfw:commentRss>http://blog.crowe.co.nz/comments/commentRss/921.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
