<?xml version="1.0" encoding="UTF-8" ?>
<rss version="0.91">
<channel><title>Logiclabz</title><link>http://www.logiclabz.com/</link>
<description>Logiclabz : Web development Tips and Tricks in Asp.Net, C#, Javascript, HTML, XML and XSLT etc.,</description><language>en-us</language>
<item>
<title>Selecting Random Rows from a Table in SQL Server</title><link>http://www.logiclabz.com/sql-server/selecting-random-rows-from-a-table-in-sql-server.aspx</link><description><![CDATA[<p>Selecting random row in a small table in sql server can be achived by NEWID() function with much affect in performance.</p>
<pre name="code" class="sql">SELECT TOP 10 * FROM Table ORDER BY NEWID()</pre>
<p><img alt="Random Row in SQL Server with NEWID function" src="/postimg/order-by-newid-upd.jpg" /></p>
<p>NEWID() generates a globally unique identifier (GUID) in memory for each row. so, when we sort by that GUID with the ORDER BY clause random row are being selected.</p>
<p>But when it comes to large tables where million of records are stored selecting random records for sampling affect performance very much.</p>
<p>Best alternative way is of using &quot;BINARY_CHECKSUM&quot; function.</p>
<pre name="code" class="sql">SELECT TOP 10 * FROM Table WHERE (ABS(CAST((BINARY_CHECKSUM(*) *RAND()) as int)) % 100) &lt; 10</pre>
<p><img alt="Random Row in SQL Server with BINARY_CHECKSUM" src="/postimg/binary-checksum-with-rand-upd.jpg" /></p>
<p>The BINARY_CHECKSUM function generates a checksum value that is based on the values of the columns that you specify.</p>
<p>If two rows are different, they typically will generate different checksum numbers.</p>
<p>The BINARY_CHECKSUM function generally is used to verify whether any of the columns in a row in a table have changed.</p>
<p>There-by when we added the RAND function to the BINARY_CHECKSUM(*) function would generate a random number for each rows as column values for each row would vastly differ.</p>
<p>(ABS(CAST((BINARY_CHECKSUM(*) *RAND()) as int)) % 100) would generate a random number between 0 and 99 for each row in the table and then choose all of those rows whose random number is less than the value of the specified percent (in this case, it is 10)</p>]]></description></item>
<item>
<title>Useful SQL Server Management Studio Keyboard Shortcuts</title><link>http://www.logiclabz.com/sql-server/useful-sql-server-management-studio-keyboard-shortcuts.aspx</link><description><![CDATA[<p><style type="text/css">
table{padding:5px}
th{padding:5px;background:#e7e5dc;font:bold 12px arial;border:1px solid #a7a497;}
td{padding:5px;border:1px solid #a7a497;}
</style></p>
<p>Please find some useful keyword shortcuts available in Microsoft SQL Server Management Studio that would help to improve your productivity on the thing done frequently.</p>
<table>
    <tbody>
        <tr>
            <th>Action</th>
            <th>KeyBoard ShortCut</th>
        </tr>
        <tr>
            <td>Display the context menu/mouse Right Click</td>
            <td>SHIFT+F10</td>
        </tr>
        <tr>
            <td>Display the&nbsp;New File&nbsp;dialog box to create a file</td>
            <td>CTRL+N</td>
        </tr>
        <tr>
            <td>Display the Query Designer</td>
            <td>CTRL+SHIFT+Q</td>
        </tr>
        <tr>
            <td>Toggle full screen mode</td>
            <td>SHIFT+ALT+ENTER</td>
        </tr>
        <tr>
            <td>Scroll text up one line</td>
            <td>CTRL+UP ARROW</td>
        </tr>
        <tr>
            <td>Scroll text down one line</td>
            <td>CTRL+DOWN ARROW</td>
        </tr>
        <tr>
            <td>Reverse the last editing action</td>
            <td>CTRL+Z</td>
        </tr>
        <tr>
            <td>Restore the previously undone edit</td>
            <td>CTRL+SHIFT+Z or ALT+SHIFT+BACKSPACE or CTRL+Y</td>
        </tr>
        <tr>
            <td>Save all</td>
            <td>CTRL+SHIFT+S</td>
        </tr>
        <tr>
            <td>Delete all text in the current file</td>
            <td>CTRL+SHIFT+DEL</td>
        </tr>
        <tr>
            <td>Display the&nbsp;Go To Line&nbsp;dialog box</td>
            <td>CTRL+G</td>
        </tr>
        <tr>
            <td>Increase line indent</td>
            <td>TAB</td>
        </tr>
        <tr>
            <td>Decrease line indent</td>
            <td>SHIFT+TAB</td>
        </tr>
        <tr>
            <td><strong>Make the selected text upper case</strong></td>
            <td><strong>CTRL+SHIFT+U</strong></td>
        </tr>
        <tr>
            <td><strong>Make the selected text lower case</strong></td>
            <td><strong>CTRL+SHIFT+L</strong></td>
        </tr>
        <tr>
            <td><strong>Make the selected text a comment</strong></td>
            <td><strong>CTRL+K, CTRL+C</strong></td>
        </tr>
        <tr>
            <td><strong>Uncomment the selected text</strong></td>
            <td><strong>CTRL+K, CTRL + U</strong></td>
        </tr>
        <tr>
            <td>Open a new query with current connection</td>
            <td>CTRL+Q</td>
        </tr>
        <tr>
            <td>Execute the selected portion of the query editor or the entire query editor if nothing is selected</td>
            <td>F5 or CTRL+E or ALT+X</td>
        </tr>
        <tr>
            <td>Display the estimated execution plan</td>
            <td>CTRL+L</td>
        </tr>
        <tr>
            <td><strong>Cancel the executing query</strong></td>
            <td><strong>ALT+BREAK</strong></td>
        </tr>
        <tr>
            <td><strong>Include actual execution plan in the query output</strong></td>
            <td><strong>CTRL+M</strong></td>
        </tr>
        <tr>
            <td>Output results in a grid</td>
            <td>CTRL+D</td>
        </tr>
        <tr>
            <td>Output results in text format</td>
            <td>CTRL+T</td>
        </tr>
        <tr>
            <td>Output results to a file</td>
            <td>CTRL+SHIFT+F</td>
        </tr>
        <tr>
            <td><strong>Show or hide the query results pane</strong></td>
            <td><strong>CTRL+R</strong></td>
        </tr>
        <tr>
            <td>Toggle between query and results pane</td>
            <td>F6</td>
        </tr>
        <tr>
            <td>Move to the next active window in Management Studio</td>
            <td>CTRL+F6</td>
        </tr>
        <tr>
            <td>Display the&nbsp;Find&nbsp;dialog box</td>
            <td>CTRL+F</td>
        </tr>
        <tr>
            <td><strong>Display the&nbsp;Replace&nbsp;dialog box</strong></td>
            <td><strong>CTRL+H</strong></td>
        </tr>
        <tr>
            <td>Start incremental search. Type the characters to search for or press CTRL+I to search for characters from the previous search</td>
            <td>CTRL+I</td>
        </tr>
        <tr>
            <td><strong>Find the next occurrence of the previous search text</strong></td>
            <td><strong>F3</strong></td>
        </tr>
        <tr>
            <td>Find the previous occurrence of the search text</td>
            <td>SHIFT+F3</td>
        </tr>
        <tr>
            <td>Find the next occurrence of the currently selected text</td>
            <td>CTRL+F3</td>
        </tr>
        <tr>
            <td>Find the previous occurrence of the currently selected text</td>
            <td>CTRL+SHIFT+F3</td>
        </tr>
        <tr>
            <td>Display the&nbsp;Replace in Files&nbsp;dialog box</td>
            <td>CTRL+SHIFT+H</td>
        </tr>
        <tr>
            <td>Reverse incremental search so it starts at the bottom of the file and searches to the top</td>
            <td>CTRL+SHIFT+I</td>
        </tr>
        <tr>
            <td>Select or clear the&nbsp;Search up&nbsp;option in&nbsp;Find and Replace</td>
            <td>ALT+F3, B</td>
        </tr>
        <tr>
            <td>Stop the&nbsp;Find in Files&nbsp;search</td>
            <td>ALT+F3, S</td>
        </tr>
        <tr>
            <td>Select or clear the&nbsp;Find whole word&nbsp;option in&nbsp;Find and Replace</td>
            <td>ALT+F3, W</td>
        </tr>
        <tr>
            <td>Selects or clears the&nbsp;Wildcard&nbsp;option in&nbsp;Find and Replace</td>
            <td>ALT+F3, P</td>
        </tr>
    </tbody>
</table>
<p>&nbsp;</p>]]></description></item>
<item>
<title>Facebook Login Error - Oauth2 specification states that 'perms' should now be called 'scope'</title><link>http://www.logiclabz.com/javascript/facebook-login-error-oauth2-specification-states-that-perms-should-now-be-called-scope.aspx</link><description><![CDATA[<p>As of December 13th, 2011, the JavaScript SDK now only supports OAuth 2.0 for authentication.     The ability to enable OAuth 2.0 in the JS SDK was first introduced in July. All     apps were given <a href="https://developers.facebook.com/docs/oauth2-https-migration/">         until October 1, 2011</a> to test and migrate. With this change, please ensure     that you replaced <code>response.session</code> with <code>response.authResponse</code>.     To ask for permissions, you must use <code>scope</code> instead of <code>perms</code>.     Read more about the specific changes <a href="https://developers.facebook.com/blog/post/525/">         here</a>.</p>
<p>https://developers.facebook.com/blog/post/525/</p>
<p>Change required:</p>
<p>1) response.session should be changed to response.authResponse</p>
<p>Old:</p>
<pre class="jscript" name="code">
FB.login(function(response) {
  if (response.session) {
    console.log(&quot;User is connected to the application.&rdquo;);
    var accessToken = response.session.access_token;
  }
});
</pre>
<p>New:</p>
<pre class="jscript" name="code">
FB.login(function(response) {
  if (response.authResponse) {
    console.log(&quot;User is connected to the application.&rdquo;);
    var accessToken = response.authResponse.accessToken;
  }
});
</pre>
<p>2)<code>auth.sessionChange</code> is deprecated and being replaced with <code>auth.authResponseChange     </code></p>
<p>3)perms should be changed to scope</p>
<p>New:</p>
<pre class="jscript" name="code">
FB.login(function(response) {
  if (response.authResponse) {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Good to see you, ' + response.name + '.');
      FB.logout(function(response) {
        console.log('Logged out.');
      });
    });
  } else {
    console.log('User cancelled login or did not fully authorize.');
  }
}, {scope: 'email'});</pre>]]></description></item>
<item>
<title>Case-sensitive check/search/match in T-SQL - Sql Server 2005</title><link>http://www.logiclabz.com/sql-server/case-sensitive-check-search-match-in-t-sql-sql-server-2005.aspx</link><description><![CDATA[<p>Generally, SQL Server is not case-sensitive i.e SQL Server doesn't check,search or matching the values with case.</p>
<p>Though there are ways to make them case-sensitive using collation at various levels i.e at Server Level, Database Level, Column Level. This would make SQL Server case sensitive at that level permanantely.</p>
<p>In some case is required to matching value with case temporarily at particular T-SQL or in an If Condition. This case-sensitive check in SQL Server can be performed with following methods.</p>
<p><strong>Method 1:</strong> <strong>Using COLLATE</strong></p>
<p>Adding COLLATE Latin1_General_CS_AS makes the search case sensitive 	 	If condition matching can be done as</p>
<pre name="code" class="sql">
	IF('Case Sensitive Sql' COLLATE SQL_Latin1_General_CP1_CS_AS='CASE SENSITIVE SQL')
		PRINT 'Matched'
	ELSE
		PRINT 'Un-Matched'
	</pre>
<p>Search can been done in T-SQL as</p>
<pre name="code" class="sql">
	SELECT mycolumn FROM mytable WHERE 
		 CAST(mycolumn AS VARBINARY(10)) = CAST('Case Sensitive Sql' AS VARBINARY(10)) 
	</pre>
<p><strong>Method 2: </strong><strong>Using VARBINARY</strong></p>
<p>Casting the given varchar value in equalent VARBINARY would determine the difference in value due to case.</p>
<pre name="code" class="sql">
	select CAST('Case Sensitive Sql' AS VARBINARY(100)) as Camelcase,CAST('CASE SENSITIVE SQL' AS VARBINARY(100)) as UpperCase
	</pre>
<pre name="code" class="sql">
	IF(CAST('Case Sensitive Sql' AS VARBINARY(100))=CAST('CASE SENSITIVE SQL' AS VARBINARY(100)))
		PRINT 'Matched'
	ELSE
		PRINT 'Un-Matched'
	</pre>
<p>Search can be done in T-SQL as</p>
<pre name="code" class="sql">
	SELECT mycolumn FROM mytable WHERE 
		 mycolumn COLLATE SQL_Latin1_General_CP1_CS_AS='Case Sensitive Sql'
	</pre>]]></description></item>
<item>
<title>Get AM/PM in Sql Server DateTime Format</title><link>http://www.logiclabz.com/sql-server/get-am-pm-in-sql-server-datetime-format.aspx</link><description><![CDATA[<p>
Sql Server natively does not provide any convert format to get AM/PM part from DateTime datatype.
</p>
<p>
The following T-SQL snippet gets the AM/PM from a DateTime format in Sql Server.
</p>
<pre name="code" class="sql">
print Right(CONVERT(VARCHAR,getdate(),9),2)
</pre>]]></description></item>
<item>
<title>Replace/Remove Special Characters in Sql Server 2005/2008</title><link>http://www.logiclabz.com/sql-server/replace-remove-special-characters-in-sql-server-2005-2008.aspx</link><description><![CDATA[<p>Sql Server dosen't have any inbuilt function for replacing special characters. We can create a function to replace/remove special characters.  Sql Function to replace special characters is follows</p>
<pre name="code" class="sql">
CREATE FUNCTION dbo.ReplaceSpecialChars (@inpStr VARCHAR(512),@repChar CHAR(1))
RETURNS VARCHAR(512)
BEGIN
	DECLARE @retStr VARCHAR(512)
	SET @retStr = ''
	IF(LEN(@inpStr) &gt; 0)
	BEGIN
		DECLARE @l INT
		SET @l = LEN(@inpStr)
		DECLARE @p INT
		SET @p = 1
		WHILE @p &lt;= @l
		BEGIN
			DECLARE @c INT
			SET @c = ASCII(SUBSTRING(@inpStr, @p, 1))
			IF @c BETWEEN 48 AND 57 OR @c BETWEEN 65 AND 90 OR @c BETWEEN 97 AND 122
				SET @retStr = @retStr + CHAR(@c)
			ELSE
				SET @retStr = @retStr + @repChar
				SET @p = @p + 1
		END
	END
RETURN @retStr
END
</pre>
<p>To replace special characters with &lsquo;-&rsquo; use the below query</p>
<pre name="code" class="sql">
Select dbo.ReplaceSpecialChars(&rsquo;English,Tamil,Hindi Movies&rsquo;,'-&rsquo;)</pre>
<p><b>Output :</b> English-Tamil-Hindi-Movies</p>
<p>To remove special characters use following query</p>
<pre name="code" class="sql">Select dbo.ReplaceSpecialChars(&rsquo;English,Tamil,Hindi Movies&rsquo;,&rdquo;)</pre>
<p><b>Output :</b> EnglishTamilHindiMovies</p>
<p>Code Sample to replace continues replace chars:</p>
<p>If you have continues special character, you will get many replace characters. to remove multiple replace character use below code before the return statement.</p>
<pre name="code" class="sql">
while charindex(@repChar+@repChar, @retStr ) &gt; 0 
begin 
select @retStr = Replace( @retStr , @repChar+@repChar,@repChar) end
</pre>
<pre name="code" class="sql">
Select dbo.ReplaceSpecialChars(&rsquo;English , Tamil , Hindi Movies&rsquo;,'-&rsquo;)</pre>
<p><b>Output :</b> &ldquo;English-Tamil-Hindi-Movies&rdquo; instead of &ldquo;English&mdash;Tamil&mdash;Hindi-Movies&rdquo;</p>]]></description></item>
<item>
<title>Search / Zoom to an address in Google Map using its API</title><link>http://www.logiclabz.com/javascript/search-zoom-to-an-address-in-google-map-using-its-api.aspx</link><description><![CDATA[<p>The following piece of script would load a google map and then search for particular address using geocoder.geocode method in Google Map API.</p>
<pre name="code" class="jscript">
   var geocoder;
   var map;
   function initialize() {
        geocoder = new google.maps.Geocoder();
        var myLatlng = new google.maps.LatLng(sLat, sLng);
        var myOptions = {
            zoom: 5,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById(&quot;map_canvas&quot;), myOptions);
	
	GoToAddress();
   }
   function GoToAddress() {
        var address = &quot;Adyar, Chennai, India&quot;; // Address to Search
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                map.setZoom(15);
            } else {
                alert(&quot;Search was not successful for the following reason: &quot; + status);
            }
        });
    }
    window.onload = function () {
        var script = document.createElement(&quot;script&quot;);
        script.type = &quot;text/javascript&quot;;
        script.src = &quot;http://maps.google.com/maps/api/js?sensor=false&amp;key=__Your_Google_Map_API_registered_Key__&amp;callback=initialize&quot;;
        document.body.appendChild(script);
    };
</pre>
<p>initialize() method would render google map with your registered <a href="http://code.google.com/apis/maps/signup.html">google map api key</a>.</p>
<p>GoToAddress() method uses geocoder.geocode in google map api for searching particular location on map. And on google.maps.GeocoderStatus.OK status, it zooms the map with desired zoom level.</p>]]></description></item>
<item>
<title>Create Linked Server in Sql Server 2005</title><link>http://www.logiclabz.com/sql-server/create-linked-server-in-sql-server-2005.aspx</link><description><![CDATA[<p>A linked server allows for access database that are placed across different servers.</p>
<p>If the linked server is defined as an instance of SQL Server, remote stored procedures can be executed.</p>
<pre class="sql" name="code">
sp_addlinkedserver [ @server= ] 'server' [ , [ @srvproduct= ] 'product_name' ]       [ , [ @provider= ] 'provider_name' ]      [ , [ @datasrc= ] 'data_source' ]       [ , [ @location= ] 'location' ]       [ , [ @provstr= ] 'provider_string' ]       [ , [ @catalog= ] 'catalog' ]   </pre>
<p>Valid provider_name for various databases</p>
<p>SQL Server 	 - SQLNCLI</p>
<p>Oracle     	 - MSDAORA</p>
<p>Access/Jet 	 - Microsoft.Jet.OLEDB.4.0</p>
<p>ODBC data source - MSDASQL</p>
<p>Sample query to create linked server in sql server 2005  for</p>
<p>Database name=dbname</p>
<p>Data Source=servername</p>
<p>User ID=dbuser</p>
<p>password=dbuserpwd</p>
<pre class="sql" name="code">
sp_addlinkedserver 'lnkservername','lnkprdname','SQLNCLI','servername,1433',NULL,NULL,NULL

sp_addlinkedsrvlogin 'lnkservername','false',NULL,'dbuser','dbuserpwd'
</pre>
<p>Query to drop an existing linked server</p>
<pre class="sql" name="code">
sp_dropserver 'lnkservername','droplogins'
</pre>]]></description></item>
<item>
<title>Alter/Change new Default Value of a Column in Sql Server 2005</title><link>http://www.logiclabz.com/sql-server/alter-change-new-default-value-of-a-column-in-already-created-table.aspx</link><description><![CDATA[<pre name="code" class="sql">
ALTER TABLE {table-name} DROP CONSTRAINT {column-constraint-name}
</pre>
<pre name="code" class="sql">
ALTER TABLE {table-name} WITH NOCHECK 
ADD CONSTRAINT [{column-constraint-name}] DEFAULT (6) FOR {column-name}
</pre>]]></description></item>
<item>
<title>T-SQL to Disable/Enable Job in SQL Server 2005</title><link>http://www.logiclabz.com/sql-server/t-sql-to-disable-enable-job-in-sql-server-2005.aspx</link><description><![CDATA[<p>The following query shows the list of jobs running on the sql server.</p>
<pre name="code" class="sql">
select * from msdb..sysjobs
</pre>
<p><input type="image" src="http://www.logiclabz.com/postimg/disable-sql-server-job-using-t-sql.gif" alt="List of Sql server Job on the server" /></p>
<p>From the msdb..sysjobs, the required job_id for enable or disable to be taken.</p>
<p>T-SQL to dis-enable specific job in sql server</p>
<pre name="code" class="sql">
 EXEC msdb..sp_update_job @job_id = '770F175B-C8A0-4157-BACD-0AF129DF570C', @enabled = 0  
</pre>
<p>T-SQL to enable specific job in sql server</p>
<pre name="code" class="sql">
 EXEC msdb..sp_update_job @job_id = '770F175B-C8A0-4157-BACD-0AF129DF570C', @enabled = 1  
</pre>
<p>&nbsp;</p>]]></description></item>
</channel></rss>
