<?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; T-SQL</title>
	<atom:link href="http://mycodetrip.com/tag/ms-sql-server-t-sql-scripts/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>Select From One Table For Data That Doesn’t Exist In Another Table</title>
		<link>http://mycodetrip.com/2009/01/06/select-from-one-table-for-data-that-doesn%e2%80%99t-exist-in-another-table_217/</link>
		<comments>http://mycodetrip.com/2009/01/06/select-from-one-table-for-data-that-doesn%e2%80%99t-exist-in-another-table_217/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 15:50:14 +0000</pubDate>
		<dc:creator>Shiva</dc:creator>
				<category><![CDATA[Tips / HowTos]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://mycodetrip.com/?p=217</guid>
		<description><![CDATA[Five different ways to query data from one table for data that doesn&#8217;t exist in another table.]]></description>
			<content:encoded><![CDATA[<p>Five different ways to query data from one table for data that doesn&#8217;t exist in another table.</p>
]]></content:encoded>
			<wfw:commentRss>http://mycodetrip.com/2009/01/06/select-from-one-table-for-data-that-doesn%e2%80%99t-exist-in-another-table_217/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>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>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>

