<?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>my code trip &#187; Code Snippet</title>
	<atom:link href="http://mycodetrip.com/tag/code-snippet-example/feed/" rel="self" type="application/rss+xml" />
	<link>http://mycodetrip.com</link>
	<description>stories from the information technology highway</description>
	<lastBuildDate>Mon, 29 Nov 2010 19:58:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>C# .Net Temperature Struct Code for Fahrenheit and Celsius Manipulation</title>
		<link>http://mycodetrip.com/2009/01/02/c-net-temperature-struct-code-for-fahrenheit-and-celsius-manipulation_209/</link>
		<comments>http://mycodetrip.com/2009/01/02/c-net-temperature-struct-code-for-fahrenheit-and-celsius-manipulation_209/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 04:54:36 +0000</pubDate>
		<dc:creator>Shiva Manjunath</dc:creator>
				<category><![CDATA[Tips / HowTos]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[Code Snippet]]></category>

		<guid isPermaLink="false">http://mycodetrip.com/?p=209</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[]]></content:encoded>
			<wfw:commentRss>http://mycodetrip.com/2009/01/02/c-net-temperature-struct-code-for-fahrenheit-and-celsius-manipulation_209/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Select Rows From Table Based On Time Value in DateTime Column</title>
		<link>http://mycodetrip.com/2008/12/22/select-query-rows-from-table-on-time-only-in-datetime-column-field-sample_183/</link>
		<comments>http://mycodetrip.com/2008/12/22/select-query-rows-from-table-on-time-only-in-datetime-column-field-sample_183/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 01:47:22 +0000</pubDate>
		<dc:creator>Shiva</dc:creator>
				<category><![CDATA[Tips / HowTos]]></category>
		<category><![CDATA[Code Snippet]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://mycodetrip.com/?p=183</guid>
		<description><![CDATA[T-SQL code snippet for selecting rows from a database table based on time values in the datetime field.]]></description>
			<content:encoded><![CDATA[<p>Recently, I was tasked with retrieving only those records from a transaction table in which the <strong>transaction datetime timestamp column</strong> <strong>had a time value </strong><strong>on or after 5:30 pm</strong>. Following is a query I came up with for doing such selects. The sample code uses the <strong>AdventureWorks sample database</strong>. I could not find any column in the database that had time values in datetime fields and ended up creating the sample data to test this query.</p>
<p>So before running this sample code, remember to run the update query to update 2 of the rows with time values, so that you can see that this query actually works. Also remember to change the <strong>datetime format</strong> number (Ex 101 for US dates) in the <strong>CONVERT </strong>methods in the SQL before using it in your code.</p>
<h2>Update 2 Rows With Time value For Testing The Select Query</h2>
<pre class="brush: sql">
use AdventureWorks
go

-- update 2 rows with time to test the select query
update Sales.SalesTerritory set ModifiedDate = &#039;1998-06-01 17:30:06.000&#039; where TerritoryID = 2
update Sales.SalesTerritory set ModifiedDate = &#039;1998-06-01 18:35:11.000&#039; where TerritoryID = 3
</pre>
<h2>Query To Select Rows Based On Time Only &#8211; US Date Format</h2>
<pre class="brush: sql">
use AdventureWorks
go

-- select only those territories whose modified time on 6/1/1998 was after 5:30 pm (date in US format)
declare @checkDateTime DATETIME
select @checkDateTime  = CONVERT(DateTime, CONVERT(Char, &#039;6/1/1998&#039;, 101), 101) + convert(datetime, &#039;17:30:00&#039;,114)
select ModifiedDate, * from Sales.SalesTerritory where  ModifiedDate  &gt;= @checkDateTime
</pre>
<h2>Query To Select Rows Based On Time Only &#8211; British / UK Date Format</h2>
<pre class="brush: sql">
use AdventureWorks
go

-- select only those territories whose modified time on 1/6/1998 was after 5:30 pm (date in our beloved queen&#039;s format)
declare @checkDateTime DATETIME
select @checkDateTime  = CONVERT(DateTime, CONVERT(Char, &#039;1/6/1998&#039;, 103), 103) + convert(datetime, &#039;17:30:00&#039;,114)
select ModifiedDate, * from Sales.SalesTerritory where  ModifiedDate  &gt;= @checkDateTime
</pre>
<p>If you want to do it for the <strong>current date</strong>, replace the hardcoded date value in the example with <strong>GETDATE()</strong> Likewise, if you want to do it for yesterdays date, or the day before yesterday, use  <strong>GETDATE() &#8211; 1</strong> or <strong>GETDATE() &#8211; 2 </strong>respectively.</p>
]]></content:encoded>
			<wfw:commentRss>http://mycodetrip.com/2008/12/22/select-query-rows-from-table-on-time-only-in-datetime-column-field-sample_183/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>T- Sql To Concatenate A Table Column’s Values Into A Csv String</title>
		<link>http://mycodetrip.com/2008/12/16/t-sql-to-concatenate-a-table-column-values-into-a-csv-string_160/</link>
		<comments>http://mycodetrip.com/2008/12/16/t-sql-to-concatenate-a-table-column-values-into-a-csv-string_160/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 19:33:11 +0000</pubDate>
		<dc:creator>Shiva</dc:creator>
				<category><![CDATA[Tips / HowTos]]></category>
		<category><![CDATA[1 Line Wonder]]></category>
		<category><![CDATA[Code Snippet]]></category>

		<guid isPermaLink="false">http://mycodetrip.com/?p=160</guid>
		<description><![CDATA[1 line T-Sql code snippet to concatenate a table column’s values into A csv string ]]></description>
			<content:encoded><![CDATA[<p>A 1 line script to concatenate all the values in a table&#8217;s column and output a single comma-separated value (CSV) string, using the all-powerful SQL Serve <strong>STUFF function</strong> (for removing the leading comma) with the <strong>FOR XML clause</strong>.</p>
<p>The code snippet below uses the <strong>Pubs</strong> sample database that comes free with <strong>MS SQL Server</strong>.</p>
<pre class="brush: sql">
USE Pubs
GO

SELECT STUFF((SELECT &#039;,&#039; + au_fname FROM Authors FOR XML PATH(&#039;&#039;)),1, 1, &#039;&#039;) AS Column2CSVString
</pre>
<p>The output of the code snippet is as follows.</p>
<pre class="brush: html">
Abraham,Reginald,Cheryl,Michel,Innes,Ann,Marjorie,Morningstar,Burt,Sheryl,Livia,Shiva,Charlene,Stearns,Heather,Michael,Sylvia,Albert,Anne,Meander,Dean,Dirk,Johnson,Akiko
</pre>
<p>To order the column values in <strong>ASC </strong>or <strong>DESC </strong>order, add the <strong>ORDER BY clause</strong> after the tablename as shown below.</p>
<pre class="brush: sql">
USE Pubs
GO

SELECT STUFF((SELECT &#039;,&#039; + au_fname FROM Authors ORDER BY 1 ASC FOR XML PATH(&#039;&#039;)),1, 1, &#039;&#039;) AS Column2CSVString
</pre>
<p>The output of the code snippet is as follows.</p>
<pre class="brush: html">
Abraham,Akiko,Albert,Ann,Anne,Burt,Charlene,Cheryl,Dean,Dirk,Heather,Innes,Johnson,Livia,Marjorie,Meander,Michael,Michel,Morningstar,Reginald,Sheryl,Shiva,Stearns,Sylvia
</pre>
<p>Finally, if you want to make this really generic and use dynamic SQL to pass in the column name and table name and delimiter, then let me know and I will post the code here.</p>
]]></content:encoded>
			<wfw:commentRss>http://mycodetrip.com/2008/12/16/t-sql-to-concatenate-a-table-column-values-into-a-csv-string_160/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Get the Number of CPUs on a ‘SQL Server’ Machine</title>
		<link>http://mycodetrip.com/2008/09/21/get-number-of-cpus-processors-on-server-machine_78/</link>
		<comments>http://mycodetrip.com/2008/09/21/get-number-of-cpus-processors-on-server-machine_78/#comments</comments>
		<pubDate>Sun, 21 Sep 2008 20:23:35 +0000</pubDate>
		<dc:creator>Shiva Manjunath</dc:creator>
				<category><![CDATA[Tips / HowTos]]></category>
		<category><![CDATA[Code Snippet]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://mycodetrip.com/?p=78</guid>
		<description><![CDATA[Script to get the number of physical and logical CPUs (processors) on a 'SQL Server' Server Machine. ]]></description>
			<content:encoded><![CDATA[<p>Script to get the number of <strong>physical </strong>and <strong>logical CPUs</strong> (processors) on a &#8216;SQL Server&#8217; Server Machine. Uses the <a href="http://msdn.microsoft.com/en-us/library/ms175048(SQL.90).aspx" target="_blank">sys.dm_os_sys_info</a> System View.</p>
<p><span style="text-decoration: underline;">Note</span>: You should run the script on the Server Machine itself. If you connect to the Server using SQL Client from your local machine, this script will show you the CPUs on your machine, not the server machine.</p>
<p><strong>Script Version</strong>:</p>
<pre class="brush: sql">
-- ------------------------------------
-- From: http://mycodetrip.com
-- URL: http://mycodetrip.com/?p=78
-- Remarks: returns the number of
--    logical and physical CPUs on
--    a &#039;sql server&#039; server machine
-- ------------------------------------
select cpu_count &#039;logical_cpus&#039;                     -- logical cpus
, (cpu_count / hyperthread_ratio) &#039;physical_cpus&#039;   -- physical cpus
from sys.dm_os_sys_info
</pre>
<p><strong>Stored Procedure Version</strong>:</p>
<pre class="brush: sql">
create procedure usp_GetServerCpusCount
as
-- ------------------------------------
-- From: http://mycodetrip.com
-- URL: http://mycodetrip.com/?p=78
-- Remarks: returns the number of
--    logical and physical CPUs on
--    a &#039;sql server&#039; server machine
-- ------------------------------------
begin
select cpu_count &#039;logical_cpus&#039;                     -- logical cpus
, (cpu_count / hyperthread_ratio) &#039;physical_cpus&#039;   -- physical cpus
from sys.dm_os_sys_info
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mycodetrip.com/2008/09/21/get-number-of-cpus-processors-on-server-machine_78/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Get Row Counts, Column Counts, Data Sizes For All Tables</title>
		<link>http://mycodetrip.com/2008/09/20/get-row-counts-column-counts-data-sizes-for-all-tables_75/</link>
		<comments>http://mycodetrip.com/2008/09/20/get-row-counts-column-counts-data-sizes-for-all-tables_75/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 16:59:29 +0000</pubDate>
		<dc:creator>Shiva Manjunath</dc:creator>
				<category><![CDATA[Tips / HowTos]]></category>
		<category><![CDATA[Code Snippet]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://mycodetrip.com/?p=75</guid>
		<description><![CDATA[T-SQL Script to get the row counts, column counts and data sizes for all the tables in a database.]]></description>
			<content:encoded><![CDATA[<p>This script will get the rows counts, column counts (number of columns) and data size for all the tables in your SQL Server database. The results are sorted by the table name in ascending order. If you want to change this sort, you can do so in the ORDER BY clause in the last line of the script. </p>
<pre class="brush: sql">
-- ---------------------------------
-- From: http://mycodetrip.com
-- URL: http://mycodetrip.com/?p=75
-- ---------------------------------
declare @tableStats table (
    table_name sysname ,
    row_count int,
    reserved_size varchar(50),
    data_size varchar(50),
    index_size varchar(50),
    unused_size varchar(50))

set nocount on

insert     @tableStats
    exec sp_msforeachtable &#039;sp_spaceused &#039;&#039;?&#039;&#039;&#039;

select     a.table_name,
    a.row_count &#039;Total Rows&#039;,
    COUNT(*) as &#039;Total Columns&#039;,
    CAST(REPLACE(a.data_size, &#039; KB&#039;, &#039;&#039;) as integer) &#039;Data Size (Kb)&#039;
from @tableStats a
    inner join information_schema.columns b
    on a.table_name collate database_default
    = b.table_name collate database_default
group by   a.table_name, a.row_count, a.data_size
order by   1 asc
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mycodetrip.com/2008/09/20/get-row-counts-column-counts-data-sizes-for-all-tables_75/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Truncate All Tables In Database Using sp_MSforeachtable</title>
		<link>http://mycodetrip.com/2008/09/19/truncate-all-tables-in-sql-server-database-using-sp_msforeachtable_71/</link>
		<comments>http://mycodetrip.com/2008/09/19/truncate-all-tables-in-sql-server-database-using-sp_msforeachtable_71/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 15:08:11 +0000</pubDate>
		<dc:creator>Shiva Manjunath</dc:creator>
				<category><![CDATA[Tips / HowTos]]></category>
		<category><![CDATA[1 Line Wonder]]></category>
		<category><![CDATA[Code Snippet]]></category>

		<guid isPermaLink="false">http://mycodetrip.com/?p=71</guid>
		<description><![CDATA[1 line script to truncate all tables in a database]]></description>
			<content:encoded><![CDATA[<p>1 line script to <strong>truncate </strong>(delete all rows without logging the transactions) all tables within a <strong>SQL Server</strong> database.</p>
<pre class="brush: sql">
exec sp_MSforeachtable &quot;truncate table ? PRINT&#039;? truncated &#039;&quot;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mycodetrip.com/2008/09/19/truncate-all-tables-in-sql-server-database-using-sp_msforeachtable_71/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get The Number Of Days In The Current Month</title>
		<link>http://mycodetrip.com/2008/09/15/get-number-of-days-in-current-month_7/</link>
		<comments>http://mycodetrip.com/2008/09/15/get-number-of-days-in-current-month_7/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 03:31:04 +0000</pubDate>
		<dc:creator>Shiva Manjunath</dc:creator>
				<category><![CDATA[Tips / HowTos]]></category>
		<category><![CDATA[Code Snippet]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://mycodetrip.com/?p=7</guid>
		<description><![CDATA[T-SQL (SQL Server) code snippet to get the number of days in the current month.]]></description>
			<content:encoded><![CDATA[<p>T-SQL code snippet to get the number of days in the current month.</p>
<pre class="brush: sql">
declare @numberOfDaysInMonth int;
declare @theDate datetime
select @theDate = &#039;02/02/2008&#039;

set @numberOfDaysInMonth = DAY(DATEADD (m, 1, DATEADD (d, 1 - DAY(@theDate), @theDate)) - 1);

print @numberOfDaysInMonth
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mycodetrip.com/2008/09/15/get-number-of-days-in-current-month_7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

