<?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>Exceptional Geeks &#187; t-sql</title>
	<atom:link href="http://exceptionalgeeks.com/blog/tag/t-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://exceptionalgeeks.com</link>
	<description></description>
	<lastBuildDate>Wed, 31 Mar 2010 18:44:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>T-SQL Snippet: Strip Time from Datetime</title>
		<link>http://exceptionalgeeks.com/bi-curious/2010/01/15/t-sql-snippet-strip-time-from-datetime/</link>
		<comments>http://exceptionalgeeks.com/bi-curious/2010/01/15/t-sql-snippet-strip-time-from-datetime/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 21:49:22 +0000</pubDate>
		<dc:creator>Pierre LaFromboise</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://3.53</guid>
		<description><![CDATA[I would have thought that this would be fairly common knowledge but I am often asked for a quick example of how to strip off the time element of a SQL datetime type. So, here is an example followed by an explanation of how and why it works: SELECT DATEADD(d,DATEDIFF(d,0,GETDATE()),0) Technically, this doesn&#8217;t strip the [...]]]></description>
			<content:encoded><![CDATA[<p>I would have thought that this would be fairly common knowledge but I am often asked for a quick example of how to strip off the time element of a SQL <em>datetime</em> type. So, here is an example followed by an explanation of how and why it works:</p>
<pre class="brush: sql; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">SELECT DATEADD(d,DATEDIFF(d,0,GETDATE()),0)</pre>
<p>Technically, this doesn&#8217;t strip the time but instead sets it to midnight of current date. So, what is this snippet doing? To start we need to know a little bit about how <em>datetime</em> is stored in SQL Server. The <em>datetime</em> type is an 8 byte type. Essentially, it is stored as two distinct 4 byte integers. The first 4 byte integer represents the number of days since the base date for SQL Server. The base date for the <em>datetime</em> type is 01/01/1900. The second 4 byte integer that represents the milliseconds since midnight.</p>
<p>With that in mind, the following snippet should return this base date:</p>
<pre class="brush: sql; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">SELECT CAST(0 as datetime)</pre>
<p>Now, back to dissecting the original snippet, I am using the built-in <em>DATEDIFF</em> function to return an integer. The integer is the number of days that have elapsed since the base date of 01/01/1900. With the knowledge of how a <em>datetime</em> is stored you might be tempted to just <em>CAST</em> directly from the <em>datetime</em> to an <em>int</em>. However, depending on the time of day, it might cause the <em>int</em> to round up. So, it is better to just rely on the <em>DATEDIFF</em>.</p>
<p>Finally, to convert this integer back to a <em>datetime</em> type I am using the <em>DATEADD</em> function. Once again, we are using the base date and adding the integer to it to give us today&#8217;s date with a midnight time component. Pretty simple, right?</p>
]]></content:encoded>
			<wfw:commentRss>http://exceptionalgeeks.com/bi-curious/2010/01/15/t-sql-snippet-strip-time-from-datetime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
