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

<channel>
	<title>SQL Server Toolkit</title>
	<atom:link href="http://www.sqlservertoolkit.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sqlservertoolkit.com</link>
	<description>SQL Server Toolkit is the best SQL Server website out there.</description>
	<lastBuildDate>Sat, 21 Nov 2009 02:36:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SQL &#8220;Union ALL&#8221; vs. &#8220;Union&#8221;</title>
		<link>http://www.sqlservertoolkit.com/uncategorized/sql-union-all-vs-union/</link>
		<comments>http://www.sqlservertoolkit.com/uncategorized/sql-union-all-vs-union/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 19:33:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.sqlservertoolkit.com/?p=66</guid>
		<description><![CDATA[Sometimes, you might need to get a record set of the UNION of two SELECT&#8217;s but you don&#8217;t want to remove duplicates.  This might happen if you were just needing a proper count of the rows or if you just wanted to check something before deleting a bunch of rows.
The simple way to do this [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, you might need to get a record set of the UNION of two SELECT&#8217;s but you don&#8217;t want to remove duplicates.  This might happen if you were just needing a proper count of the rows or if you just wanted to check something before deleting a bunch of rows.</p>
<p>The simple way to do this is with UNION ALL.</p>
<p>A UNION statement returns the UNION of two SELECT&#8217;s but removes duplicates.  If you use UNION ALL, the records returned are all the records including duplicates.</p>



Share Is Caring:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Funcategorized%2Fsql-union-all-vs-union%2F&amp;title=SQL%20%22Union%20ALL%22%20vs.%20%22Union%22" title="Digg"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.sqlservertoolkit.com%2Funcategorized%2Fsql-union-all-vs-union%2F&amp;t=SQL%20%22Union%20ALL%22%20vs.%20%22Union%22" title="Facebook"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.sqlservertoolkit.com%2Funcategorized%2Fsql-union-all-vs-union%2F&amp;title=SQL%20%22Union%20ALL%22%20vs.%20%22Union%22" title="Google"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google" alt="Google" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Funcategorized%2Fsql-union-all-vs-union%2F&amp;title=SQL%20%22Union%20ALL%22%20vs.%20%22Union%22&amp;source=SQL+Server+Toolkit+SQL+Server+Toolkit+is+the+best+SQL+Server+website+out+there.&amp;summary=Sometimes%2C%20you%20might%20need%20to%20get%20a%20record%20set%20of%20the%20UNION%20of%20two%20SELECT%27s%20but%20you%20don%27t%20want%20to%20remove%20duplicates.%C2%A0%20This%20might%20happen%20if%20you%20were%20just%20needing%20a%20proper%20count%20of%20the%20rows%20or%20if%20you%20just%20wanted%20to%20check%20something%20before%20deleting%20a%20bun" title="LinkedIn"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.sqlservertoolkit.com%2Funcategorized%2Fsql-union-all-vs-union%2F&amp;t=SQL%20%22Union%20ALL%22%20vs.%20%22Union%22" title="MySpace"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Funcategorized%2Fsql-union-all-vs-union%2F&amp;title=SQL%20%22Union%20ALL%22%20vs.%20%22Union%22" title="StumbleUpon"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.sqlservertoolkit.com%2Funcategorized%2Fsql-union-all-vs-union%2F" title="Technorati"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://twitter.com/home?status=http%3A%2F%2Fwww.sqlservertoolkit.com%2Funcategorized%2Fsql-union-all-vs-union%2F" title="TwitThis"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/twitter.gif" title="TwitThis" alt="TwitThis" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.sqlservertoolkit.com/uncategorized/sql-union-all-vs-union/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using TIMESTAMP datatype to reduce database corruption</title>
		<link>http://www.sqlservertoolkit.com/sql-tips-and-tricks/using-timestamp-datatype-to-reduce-database-corruption/</link>
		<comments>http://www.sqlservertoolkit.com/sql-tips-and-tricks/using-timestamp-datatype-to-reduce-database-corruption/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 03:47:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.sqlservertoolkit.com/?p=59</guid>
		<description><![CDATA[There will be times when 2 people are trying to change the same record at the same time.  For example, a Human Resources Manager might want to change an Employee&#8217;s salary and an IT person might want to update the Employee&#8217;s password.  This is not an uncommon problem and applications can be written to help [...]]]></description>
			<content:encoded><![CDATA[<p>There will be times when 2 people are trying to change the same record at the same time.  For example, a Human Resources Manager might want to change an Employee&#8217;s salary and an IT person might want to update the Employee&#8217;s password.  This is not an uncommon problem and applications can be written to help avoid this but there is no way it can be prevented 100% of the time.  Therefore, it is a good idea to use the tools provided by SQL to catch this situation and alert the application.  Let&#8217;s discuss one concept.<span id="more-59"></span></p>
<p>One method is to use a &#8220;TIMESTAMP&#8221; field in the database table.  The &#8220;TIMESTAMP&#8221; field is automatically updated each time the record is updated.  Then the UPDATE Stored Procedure would check to see if the &#8220;TIMESTAMP&#8221; has changed since the record was first read by the application.  If so, then it aborts the UPDATE and alerts the calling application.  The key is that this &#8220;TIMESTAMP&#8221; value must be always returned by the &#8220;SELECT&#8221; Stored Procedures so the calling application can provide it to the UPDATE Store Procedure for comparison.  For example, below is a short Update Stored Procedure that updates &#8220;Salary&#8221; or &#8220;Password&#8221; or both:</p>
<blockquote><p>CREATE  PROCEDURE UpdEmployee<br />
@PASSWORD VARCHAR(20) = NULL,<br />
@Salary  DECIMAL(14,4) = NULL,<br />
@EmployeeID INT,<br />
@RecVer  BIGINT</p>
<p>DECLARE <br />
@QueryCheck CHAR(20),<br />
@ErrorNum INT,<br />
@RowCount INT</p>
<p>BEGIN TRANSACTION</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
&#8211; Update the Employee record specified by the Primary Key<br />
&#8211;  (@EmployeeID) with the values if specified.  Note the<br />
&#8211; COALESCE statement allows the @Password and @Salary<br />
&#8211; parameters to be optional and if not specified (NULL) then<br />
&#8211; the original values will be written back.  Also notice the<br />
&#8211; RecVer is compared to the existing RecVer in the table<br />
&#8211; and if not the same then the update is not executed.  We<br />
&#8211; cast the the RecVer from BIGINT to TIMESTAMP to make<br />
&#8211; the comparison proper since we don&#8217;t pass TIMESTAMP as<br />
&#8211; a parameter.  The SELECT procedure would return this value<br />
&#8211; as a BIGINT to the caller since it is easier to store it as such<br />
&#8211; in most programming languages.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
UPDATE Employee<br />
SET Password = COALESCE(@Password, Password),<br />
 Salary = COALECE(@Salary, Salary)<br />
WHERE EmployeeID = @EmployeeID<br />
 AND RecVer = CAST(@RecVer AS timestamp)</p>
<p>SET @QueryCheck  = CAST(@@ERROR AS char(10)) +<br />
                                            CAST(@@ROWCOUNT AS char(10))<br />
SET @ErrorNum   = CAST(LEFT(@QueryCheck, 10) AS int)<br />
SET @RowCount   = CAST(RIGHT(@QueryCheck, 10) AS int)</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
&#8211; Checking if we had an error.  We always do this for every<br />
&#8211; UPDATE or INSERT but this has nothing to do with this<br />
&#8211; article but I put it here for reference.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
IF @ErrorNum &lt;&gt;  0<br />
 BEGIN<br />
  ROLLBACK TRANSACTION<br />
  RAISERROR(@ErrorNum, 16, 1)<br />
  RETURN @ErrorNum<br />
 END<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
&#8211; If the specified row was not updated, @RowCount = 0,<br />
&#8211; then the RecVer was not the same so alert the calling<br />
&#8211; application so they can tell user and maybe refresh the<br />
&#8211; data again in their application.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
IF @RowCount &lt;&gt; 1<br />
 BEGIN<br />
  ROLLBACK TRANSACTION<br />
  RAISERROR( &#8216;Update Problem&#8217;, 16, 2)<br />
  RETURN 123456<br />
 END<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
&#8211; If you got this far, everything is OK, so commit change<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
COMMIT TRANSACTION</p>
<p>AS</p>
<p>RETURN (0)</p></blockquote>
<p> The calling application can then just provide &#8220;TRY/CATCH&#8221; blocks around the call to the SQL Server Stored Procedure and if the procedure throws an error the Return value &#8220;123456&#8242; would indicate that it was due to an attempt to modify data that had been modified since first reading.  The application has to be written to handle this situation, but at least the database provides safeguards against this sort of situation from corrupting your data by using the &#8220;TIMESTAMP&#8221; datatype.</p>



Share Is Caring:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-timestamp-datatype-to-reduce-database-corruption%2F&amp;title=Using%20TIMESTAMP%20datatype%20to%20reduce%20database%20corruption" title="Digg"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-timestamp-datatype-to-reduce-database-corruption%2F&amp;t=Using%20TIMESTAMP%20datatype%20to%20reduce%20database%20corruption" title="Facebook"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-timestamp-datatype-to-reduce-database-corruption%2F&amp;title=Using%20TIMESTAMP%20datatype%20to%20reduce%20database%20corruption" title="Google"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google" alt="Google" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-timestamp-datatype-to-reduce-database-corruption%2F&amp;title=Using%20TIMESTAMP%20datatype%20to%20reduce%20database%20corruption&amp;source=SQL+Server+Toolkit+SQL+Server+Toolkit+is+the+best+SQL+Server+website+out+there.&amp;summary=There%20will%20be%20times%20when%202%20people%20are%20trying%20to%20change%20the%20same%20record%20at%20the%20same%20time.%C2%A0%20For%20example%2C%20a%20Human%20Resources%20Manager%20might%20want%20to%20change%20an%20Employee%27s%20salary%20and%20an%20IT%20person%20might%20want%20to%20update%20the%20Employee%27s%20password.%C2%A0%20This%20is%20not%20a" title="LinkedIn"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-timestamp-datatype-to-reduce-database-corruption%2F&amp;t=Using%20TIMESTAMP%20datatype%20to%20reduce%20database%20corruption" title="MySpace"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-timestamp-datatype-to-reduce-database-corruption%2F&amp;title=Using%20TIMESTAMP%20datatype%20to%20reduce%20database%20corruption" title="StumbleUpon"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-timestamp-datatype-to-reduce-database-corruption%2F" title="Technorati"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://twitter.com/home?status=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-timestamp-datatype-to-reduce-database-corruption%2F" title="TwitThis"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/twitter.gif" title="TwitThis" alt="TwitThis" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.sqlservertoolkit.com/sql-tips-and-tricks/using-timestamp-datatype-to-reduce-database-corruption/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GETUTCDATE vs. GETDATE with timezones and daylight savings</title>
		<link>http://www.sqlservertoolkit.com/sql-tips-and-tricks/getutcdate-vs-getdate-with-timezones-and-daylight-savings/</link>
		<comments>http://www.sqlservertoolkit.com/sql-tips-and-tricks/getutcdate-vs-getdate-with-timezones-and-daylight-savings/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 03:43:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[Solutions to Unusual Problems]]></category>

		<guid isPermaLink="false">http://www.sqlservertoolkit.com/?p=57</guid>
		<description><![CDATA[In a recent project, we had to deal with SQL Server transactions that involved multiple timezones and required us to calculate the time employee&#8217;s worked across a 24&#215;7 retail chain.  Every time someone clocked-in or clocked-out we recorded the current time so we could later calculate the hours worked.  The problem was that the original [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent project, we had to deal with SQL Server transactions that involved multiple timezones and required us to calculate the time employee&#8217;s worked across a 24&#215;7 retail chain.  Every time someone clocked-in or clocked-out we recorded the current time so we could later calculate the hours worked.  The problem was that the original developers had used the &#8220;DATETIME&#8221; field but had placed the default to be &#8220;GETDATE()&#8221; which uses the current time of the server.  Therefore,  in order to accurately calculate the time difference two times requires complicated daylight savings time lookup tables and more effort in the User Interface.  Here is how we solved it.<span id="more-57"></span></p>
<p>We were able to find an easy way to solve this problem.  We converted all entries to use &#8220;GETUTCDATE()&#8221; which is the current UTC time which does not vary by time of year or timezone.  Therefore, all the time was based off the same timezone and it never changed for daylight savings.  When computing people&#8217;s paycheck and summing the hours worked we were able to perform simple calculations and could guarantee the correct number of hours no matter where they clocked-in or clocked-out or whether they worked during a daylight savings time change.<br />
Using GETUTCDATE instead of GETDATE was a simple solution to a complicated problem to solve timezones and daylight savings changes.</p>



Share Is Caring:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fgetutcdate-vs-getdate-with-timezones-and-daylight-savings%2F&amp;title=GETUTCDATE%20vs.%20GETDATE%20with%20timezones%20and%20daylight%20savings" title="Digg"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fgetutcdate-vs-getdate-with-timezones-and-daylight-savings%2F&amp;t=GETUTCDATE%20vs.%20GETDATE%20with%20timezones%20and%20daylight%20savings" title="Facebook"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fgetutcdate-vs-getdate-with-timezones-and-daylight-savings%2F&amp;title=GETUTCDATE%20vs.%20GETDATE%20with%20timezones%20and%20daylight%20savings" title="Google"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google" alt="Google" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fgetutcdate-vs-getdate-with-timezones-and-daylight-savings%2F&amp;title=GETUTCDATE%20vs.%20GETDATE%20with%20timezones%20and%20daylight%20savings&amp;source=SQL+Server+Toolkit+SQL+Server+Toolkit+is+the+best+SQL+Server+website+out+there.&amp;summary=In%20a%20recent%20project%2C%20we%20had%20to%20deal%20with%20SQL%20Server%20transactions%20that%20involved%20multiple%20timezones%20and%20required%20us%20to%20calculate%20the%20time%20employee%27s%20worked%20across%20a%2024x7%20retail%20chain.%C2%A0%20Every%20time%20someone%20clocked-in%20or%20clocked-out%20we%20recorded%20the%20curre" title="LinkedIn"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fgetutcdate-vs-getdate-with-timezones-and-daylight-savings%2F&amp;t=GETUTCDATE%20vs.%20GETDATE%20with%20timezones%20and%20daylight%20savings" title="MySpace"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fgetutcdate-vs-getdate-with-timezones-and-daylight-savings%2F&amp;title=GETUTCDATE%20vs.%20GETDATE%20with%20timezones%20and%20daylight%20savings" title="StumbleUpon"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fgetutcdate-vs-getdate-with-timezones-and-daylight-savings%2F" title="Technorati"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://twitter.com/home?status=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fgetutcdate-vs-getdate-with-timezones-and-daylight-savings%2F" title="TwitThis"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/twitter.gif" title="TwitThis" alt="TwitThis" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.sqlservertoolkit.com/sql-tips-and-tricks/getutcdate-vs-getdate-with-timezones-and-daylight-savings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using SQL Server Managment Studio SQL Icon to update records</title>
		<link>http://www.sqlservertoolkit.com/sql-tips-and-tricks/using-sql-server-managment-studio-sql-icon-to-update-records/</link>
		<comments>http://www.sqlservertoolkit.com/sql-tips-and-tricks/using-sql-server-managment-studio-sql-icon-to-update-records/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 03:32:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Server Query Analyzer]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.sqlservertoolkit.com/?p=55</guid>
		<description><![CDATA[While working with SQL Server, I Often find myself needing to just change 1 value in a database record on a production server.  Usually, this happens after working with SQL Query Analzyer to research a problem and then realize that I can&#8217;t select the resulting rows below to fix the wrong value.  So how do [...]]]></description>
			<content:encoded><![CDATA[<p>While working with SQL Server, I Often find myself needing to just change 1 value in a database record on a production server.  Usually, this happens after working with SQL Query Analzyer to research a problem and then realize that I can&#8217;t select the resulting rows below to fix the wrong value.  So how do you do it quickly?  There are a few ways to do this.<span id="more-55"></span></p>
<p>One wat would be to open up SQL Query Analzyer in SQL Server and write an &#8220;UPDATE&#8221; statement but this always worries me because an &#8220;UPDATE&#8221; statement can be very dangerous if not properly formed and when you are in a hurry.  Imagine updating a password for a database of 300,000 users and forgetting to put the &#8220;WHERE&#8221; clause and whoops, you just reset everyone&#8217;s password.  This actually happened to me once when I was in a hurry and I was lucky that I had executed the UPDATE statement on a copy of the production database before trying it on the real one.</p>
<p>My preferred method when doing a quick one-time change is to use SQL Server Management Studio for SQL Server 2005 or 2008 and &#8220;right-click&#8221; my mouse on the particular table and choose &#8220;open&#8221;.  Depending on the size of the recordset, I might stop the listing or let it continue.  I then select the &#8220;SQL&#8221; icon in the toolbar in the upper menu to open the database table in SQL mode to allow me to write SQL statements to limit the resulting recordset.  By default the SQL displayed in the SQL window is the standard &#8220;SELECT&#8221; with all rows.  I then add my &#8220;WHERE&#8221; clause such as &#8220;WHERE username=&#8217;gotchanow&#8217; if wanting to update the password for that particular user.  Once it displays the row or rows based on the updated SQL statement, I then select the field to change, type in the new value and then apply the change.</p>
<p>So this is my recommended way of making a safe and simple change to a row or rows in a database without risking a mistake with an &#8220;UPDATE&#8221; statement.  I&#8217;m sure there are other ways using SQL Server 2005 or SQL Server 2008 Management Studio, and I would welcome hearing from you so we can share your suggestions with others.</p>



Share Is Caring:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-sql-server-managment-studio-sql-icon-to-update-records%2F&amp;title=Using%20SQL%20Server%20Managment%20Studio%20SQL%20Icon%20to%20update%20records" title="Digg"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-sql-server-managment-studio-sql-icon-to-update-records%2F&amp;t=Using%20SQL%20Server%20Managment%20Studio%20SQL%20Icon%20to%20update%20records" title="Facebook"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-sql-server-managment-studio-sql-icon-to-update-records%2F&amp;title=Using%20SQL%20Server%20Managment%20Studio%20SQL%20Icon%20to%20update%20records" title="Google"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google" alt="Google" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-sql-server-managment-studio-sql-icon-to-update-records%2F&amp;title=Using%20SQL%20Server%20Managment%20Studio%20SQL%20Icon%20to%20update%20records&amp;source=SQL+Server+Toolkit+SQL+Server+Toolkit+is+the+best+SQL+Server+website+out+there.&amp;summary=While%20working%20with%20SQL%20Server%2C%20I%20Often%20find%20myself%20needing%20to%20just%20change%201%20value%20in%20a%20database%20record%20on%20a%20production%20server.%C2%A0%20Usually%2C%20this%20happens%20after%20working%20with%20SQL%20Query%20Analzyer%20to%20research%20a%20problem%20and%20then%20realize%20that%20I%20can%27t%20select%20th" title="LinkedIn"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-sql-server-managment-studio-sql-icon-to-update-records%2F&amp;t=Using%20SQL%20Server%20Managment%20Studio%20SQL%20Icon%20to%20update%20records" title="MySpace"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-sql-server-managment-studio-sql-icon-to-update-records%2F&amp;title=Using%20SQL%20Server%20Managment%20Studio%20SQL%20Icon%20to%20update%20records" title="StumbleUpon"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-sql-server-managment-studio-sql-icon-to-update-records%2F" title="Technorati"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://twitter.com/home?status=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fusing-sql-server-managment-studio-sql-icon-to-update-records%2F" title="TwitThis"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/twitter.gif" title="TwitThis" alt="TwitThis" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.sqlservertoolkit.com/sql-tips-and-tricks/using-sql-server-managment-studio-sql-icon-to-update-records/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL Server Database Mirroring vs. Double-Take</title>
		<link>http://www.sqlservertoolkit.com/sql-server-database/sql-server-database-mirroring-vs-double-take/</link>
		<comments>http://www.sqlservertoolkit.com/sql-server-database/sql-server-database-mirroring-vs-double-take/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 20:42:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[High Availability]]></category>
		<category><![CDATA[SQL Server Database]]></category>
		<category><![CDATA[SQL Server Management]]></category>

		<guid isPermaLink="false">http://www.sqlservertoolkit.com/?p=48</guid>
		<description><![CDATA[We regularly get questions about what is the best way to replicate or mirror SQL Server databases to protect against disaster and to provide high availability.  There are many options out there and there is no right answer, but I thought I would address 2 we have used (SQL Server Database Mirroring and Double-Take) and provide some [...]]]></description>
			<content:encoded><![CDATA[<p>We regularly get questions about what is the best way to replicate or mirror SQL Server databases to protect against disaster and to provide high availability.  There are many options out there and there is no right answer, but I thought I would address 2 we have used (SQL Server Database Mirroring and Double-Take) and provide some input as to what we&#8217;ve found.<span id="more-48"></span></p>
<p><strong><span style="text-decoration: underline;">SQL Server 2005/2008 Database Mirroring</span>:  (Recommended)</strong></p>
<p><strong><em>Overview:</em></strong>  This works by having SQL Server write each transaction to both SQL Server databases at the same time on 2 different servers so that they are synchronized.  Depending on the mode you choose, the transactions are committed either synchronously or asynchronously.</p>
<p><em><strong>Pros:  </strong></em>The advantage of this option is that it is a standard feature of SQL Server and its free.  It has 3 modes for those that desire performance and those that must have reliability.  The 1st mode is the synchronous mode with automatic  failover (High availability and Reliability), the 2nd mode is asynchronous without automatic failover (High Performance) and the 3rd mode is synchronous without automatic failover (High Reliability).  The 1st mode requires a witness server to establish a quorum in order for the SQL Servers to know who is the primary and who is the secondary.  A nice thing about this mirroring solution, and something we appreciate, is the simple user interface.  It is so simple that even a novice IT person should be able to figure it out.</p>
<p><strong><em>Cons: </em></strong> The mirroring is on a database by database basis so it is possible that you have the primary and secondary for a group of databases spread across the 2 SQL Servers.  This is not much of a problem if you have 2 reliable servers equally paired and you can always fail over the individual databases as needed if you prefer them to be in sync on the same server.</p>
<p><strong><em>Configuration Point:</em></strong>  Depending on your needs, it is in my opinion, to configure SQL Server Database Mirroring in either the 1st or 3rd mode depending if you want automatic failover or not.  We have seen issues with database corruption with the 2nd option, but the 1st and 3rd options prevent this.  In our situation we prefer the 1st mode because we desire immediate failover and our software is written using ADO which is designed to handle failing over if the 1st connection fails.  Also, the witness server doesn&#8217;t have to be an expensive server since it is just designed to provide a quorum.</p>
<p><strong><span style="text-decoration: underline;">Double-Take</span>:</strong> </p>
<p><strong><em>Overview</em></strong>:  This is a block level file replication option that works by monitoring the database and log files and replicating any changes in them to the other server.  Their marketing material says that the change is at a very low I/O level thereby &#8220;almost&#8221; guaranteeing no file corruption but if you read the fine print, they can&#8217;t guarantee no corruption.</p>
<p><strong><em>Pros</em></strong>:  They are considered an industry standard and have a long track record.  If you talk to most IT people and ask them about the company, you&#8217;ll hear nothing but glowing remarks.  The replication is more than just SQL Databases so you can use the product to replicate regular files too.  Also, this is supposedly a great option for replicating over a WAN, but that was not our need.</p>
<p><strong><em>Cons</em></strong>:  Expensive option compared to SQL Server Database Mirroring.  We purchased 4 licenses with SQL Server replication and it cost us about $12,000 in the Fall of 2008.  The actual price may have varied somewhat but it was pretty close.  The configuration was not simple and we had to have 2 DBA&#8217;s on the phone with Double-Take tech support for 2 days trying to get our simple configuration working.  At the time there was an issue with the 64-bit version but this has been fixed.   Also, there wasn&#8217;t one program or interface to configure it all and it was confusing.  To Double-Take&#8217;s defense, the tech support people were available and very helpful.  One negative for us, which some would say is a positive, was the switch from Secondary back to Primary was a manual process that took a few steps and required a delay while the Secondary was replicated back to the Primary.</p>
<p><strong><span style="text-decoration: underline;">Writer&#8217;s Comment</span></strong>:  One unfortunate moment happened after running Double-Take for about 1 month.  We had a hardware failure on the primary SQL Server and discovered that the Double-Take failed to switch over and therefore, our applications went down.  We also discovered that the replication had stopped earlier for some reason and therefore, the database on the secondary server was completely out of sync.  Fortunately, we were able to recover the databases from our Primary server&#8217;s hard drives and install them on another server.  This failure caused us to be down for several hours while we were struggling with it.  This incident was both frustrating and scary because after spending $12,000 dollars on a product to protect against a failure, it failed to work itself.  Some might say that it was our configuration that was at fault, and while I never want to say it was never our fault, remember that Double-Take helped us configure it over a 2 day period.  The end result of this was that we decided to look for another solution.</p>
<p><strong><em>Configuration Point</em></strong>:  We installed the Double-Take on 2 servers being replicated.  These servers were Dell 2950&#8217;s running 64-bit Server 2008 and SQL Server 2005.  The databases being replicated were small and few in number.  The average size was less than 1GB and we probably only replicated about 5 databases.</p>
<p> <strong><span style="text-decoration: underline;">Summary</span></strong>:  As I said earlier, there is no right answer.  Also, I&#8217;m sure Double-Take would disagree with my earlier statements and they are more than willing to disagree but this was our experience.  We deal with sensitive information and we lost confidence in Double-Take and have been pleased with SQL Server Database Mirroring (Replication).  I&#8217;m not saying don&#8217;t buy Double-Take because it might be the perfect solution for you, but I&#8217;m saying that we had issues and prefer the simple and integrated solution of SQL Server Database Mirroring.</p>



Share Is Caring:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-server-database%2Fsql-server-database-mirroring-vs-double-take%2F&amp;title=SQL%20Server%20Database%20Mirroring%20vs.%20Double-Take" title="Digg"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-server-database%2Fsql-server-database-mirroring-vs-double-take%2F&amp;t=SQL%20Server%20Database%20Mirroring%20vs.%20Double-Take" title="Facebook"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-server-database%2Fsql-server-database-mirroring-vs-double-take%2F&amp;title=SQL%20Server%20Database%20Mirroring%20vs.%20Double-Take" title="Google"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google" alt="Google" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-server-database%2Fsql-server-database-mirroring-vs-double-take%2F&amp;title=SQL%20Server%20Database%20Mirroring%20vs.%20Double-Take&amp;source=SQL+Server+Toolkit+SQL+Server+Toolkit+is+the+best+SQL+Server+website+out+there.&amp;summary=We%20regularly%20get%20questions%20about%20what%20is%20the%20best%20way%20to%20replicate%20or%20mirror%C2%A0SQL%20Server%20databases%20to%20protect%20against%20disaster%20and%20to%20provide%20high%20availability.%C2%A0%20There%20are%20many%20options%20out%20there%20and%20there%20is%20no%20right%20answer%2C%20but%20I%20thought%20I%20would%20ad" title="LinkedIn"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-server-database%2Fsql-server-database-mirroring-vs-double-take%2F&amp;t=SQL%20Server%20Database%20Mirroring%20vs.%20Double-Take" title="MySpace"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-server-database%2Fsql-server-database-mirroring-vs-double-take%2F&amp;title=SQL%20Server%20Database%20Mirroring%20vs.%20Double-Take" title="StumbleUpon"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-server-database%2Fsql-server-database-mirroring-vs-double-take%2F" title="Technorati"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://twitter.com/home?status=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-server-database%2Fsql-server-database-mirroring-vs-double-take%2F" title="TwitThis"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/twitter.gif" title="TwitThis" alt="TwitThis" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.sqlservertoolkit.com/sql-server-database/sql-server-database-mirroring-vs-double-take/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Aggregate BIT Columns with Group By</title>
		<link>http://www.sqlservertoolkit.com/sql-tips-and-tricks/sql-aggregate-bit-columns-with-group-by/</link>
		<comments>http://www.sqlservertoolkit.com/sql-tips-and-tricks/sql-aggregate-bit-columns-with-group-by/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 03:43:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.sqlservertoolkit.com/?p=44</guid>
		<description><![CDATA[A common problem you run across in SQL programming is the need to group a series of records that contain bit fields and you need to determine the appropriate value.  If you were to try it using the MAX/MIN operators to achieve the equivalent AND/OR result of the bit field or even the SUM operator you will [...]]]></description>
			<content:encoded><![CDATA[<p>A common problem you run across in SQL programming is the need to group a series of records that contain bit fields and you need to determine the appropriate value.  If you were to try it using the MAX/MIN operators to achieve the equivalent AND/OR result of the bit field or even the SUM operator you will get one of the following errors.<span id="more-44"></span></p>
<blockquote><p>Operand data type bit is invalid for max operator<br />
or<br />
Operand data type bit is invalid for min operator<br />
or<br />
Operand data type bit is invalid for sum operator</p></blockquote>
<p> The answer to the problem is to first cast the bit field to an integer (INT) datatype and perform the MAX or MIN operator on that and then cast it back to a BIT field.</p>
<p>For example suppose you have 3 record sets from a table called &#8220;Stores&#8221; with a bit field called &#8220;ObserveDaylightSavings&#8221; and your goal is to determine if any of the records have this bit set to &#8220;1&#8243; or true, meaning that at least one of the stores observes daylight savings time.  This is the equivalent tothe &#8220;OR&#8221; logic operator where if any of the colums are true then the result is true.  The SQL to determine this is as follows:</p>
<blockquote><p>SELECT CAST(MAX(CAST(ObserveDaylightSavings as INT)) AS BIT) AS ObserveDaylightSavings<br />
FROM Stores</p></blockquote>
<p>The result would be &#8220;1&#8243; if any of the records has the value set to &#8220;1&#8243; or &#8220;true&#8221; or &#8220;0&#8243; if none of them had a bit value of &#8220;1&#8243;.</p>
<p>The reverse is just as simple.  For example if you needed to determine if all of the stores observed daylight savings time, then this would be the equivalent &#8220;AND&#8221; logical operator.  Meaning if all of the stores had the &#8220;ObserveDaylightSavings&#8221; bit field set to &#8220;1&#8243; or &#8220;true&#8221; then the result of the query would be &#8220;1&#8243;.  If any one of them did not have the field set to &#8220;1&#8243;, then the resulting column would be &#8220;0&#8243; or &#8220;false&#8221;.  The SQL to do that is as follows:</p>
<blockquote><p>SELECT CAST(MIN(CAST(ObserveDaylightSavings as INT)) AS BIT) AS ObserveDaylightSavings<br />
FROM Stores</p></blockquote>
<p>The only difference is that the MAX was replaced with the MIN operator.</p>
<p>So basically, if you are faced with the error:  &#8220;Operand data type bit is invalid for max operator&#8221; or &#8220;Operand data type bit is invalid for min operator&#8221; or even &#8220;Operand data type bit is invalid for sum operator&#8221; then the answer is to cast the value to an INT and then back to a BIT and you&#8217;ll achieve an equivalent AND/OR type of solution for the field.  This allows you to aggregate bit fields in SQL.</p>



Share Is Caring:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fsql-aggregate-bit-columns-with-group-by%2F&amp;title=SQL%20Aggregate%20BIT%20Columns%20with%20Group%20By" title="Digg"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fsql-aggregate-bit-columns-with-group-by%2F&amp;t=SQL%20Aggregate%20BIT%20Columns%20with%20Group%20By" title="Facebook"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fsql-aggregate-bit-columns-with-group-by%2F&amp;title=SQL%20Aggregate%20BIT%20Columns%20with%20Group%20By" title="Google"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google" alt="Google" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fsql-aggregate-bit-columns-with-group-by%2F&amp;title=SQL%20Aggregate%20BIT%20Columns%20with%20Group%20By&amp;source=SQL+Server+Toolkit+SQL+Server+Toolkit+is+the+best+SQL+Server+website+out+there.&amp;summary=A%20common%20problem%20you%20run%20across%20in%20SQL%C2%A0programming%20is%20the%20need%20to%20group%20a%20series%20of%20records%20that%20contain%20bit%20fields%20and%20you%20need%20to%20determine%20the%20appropriate%20value.%C2%A0%20If%20you%20were%20to%20try%20it%20using%20the%20MAX%2FMIN%20operators%20to%20achieve%20the%C2%A0equivalent%20AND%2FO" title="LinkedIn"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fsql-aggregate-bit-columns-with-group-by%2F&amp;t=SQL%20Aggregate%20BIT%20Columns%20with%20Group%20By" title="MySpace"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fsql-aggregate-bit-columns-with-group-by%2F&amp;title=SQL%20Aggregate%20BIT%20Columns%20with%20Group%20By" title="StumbleUpon"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fsql-aggregate-bit-columns-with-group-by%2F" title="Technorati"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://twitter.com/home?status=http%3A%2F%2Fwww.sqlservertoolkit.com%2Fsql-tips-and-tricks%2Fsql-aggregate-bit-columns-with-group-by%2F" title="TwitThis"><img src="http://www.sqlservertoolkit.com/wp-content/plugins/sociable/images/twitter.gif" title="TwitThis" alt="TwitThis" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.sqlservertoolkit.com/sql-tips-and-tricks/sql-aggregate-bit-columns-with-group-by/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
