<?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>Pressing the Red Button &#187; sql</title>
	<atom:link href="http://www.pressthered.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pressthered.com</link>
	<description>Adventures in Software Development</description>
	<lastBuildDate>Sun, 05 Sep 2010 16:55:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Adding Dates and Times in Python</title>
		<link>http://www.pressthered.com/adding_dates_and_times_in_python/</link>
		<comments>http://www.pressthered.com/adding_dates_and_times_in_python/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 06:11:03 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=645</guid>
		<description><![CDATA[Using the built-in modules datetime and timedelta, you can perform date and time addition/subtraction in python:

from datetime import datetime
from datetime import timedelta

#Add 1 day
print datetime.now() + timedelta(days=1)

#Subtract 60 seconds
print datetime.now() - timedelta(seconds=60)

#Add 2 years
print datetime.now() + timedelta(days=730)

#Other Parameters you can pass in to timedelta:
# days, seconds, microseconds,
# milliseconds, minutes, hours, weeks

#Pass multiple parameters (1 day [...]]]></description>
			<content:encoded><![CDATA[<p>Using the built-in modules <strong>datetime</strong> and <strong>timedelta</strong>, you can perform date and time addition/subtraction in <strong>python</strong>:</p>
<pre name="code" class="python">
from datetime import datetime
from datetime import timedelta

#Add 1 day
print datetime.now() + timedelta(days=1)

#Subtract 60 seconds
print datetime.now() - timedelta(seconds=60)

#Add 2 years
print datetime.now() + timedelta(days=730)

#Other Parameters you can pass in to timedelta:
# days, seconds, microseconds,
# milliseconds, minutes, hours, weeks

#Pass multiple parameters (1 day and 5 minutes)
print datetime.now() + timedelta(days=1,minutes=5)
</pre>
<p>Here is a python reference that gives more examples and advanced features:<br />
<a href="http://docs.python.org/library/datetime.html">http://docs.python.org/library/datetime.html</a></p>
<p>If you are coming from a .net or sql environment, here are the above examples in C# and SQL (Microsoft) for comparison:<br />
<strong>C#</strong></p>
<pre name="code" class="c#">
DateTime myTime = new DateTime();

--Add 1 day
myTime.AddDays(1);

--Subtract 60 seconds
myTime.AddSeconds(-60);

--Add 2 years
myTime.AddYears(2);
</pre>
<p><strong>SQL</strong></p>
<pre name="code" class="sql">
--Add 1 day
select DATEADD(day, 1, getdate())

--Subtract 60 seconds
select DATEADD(second, -60, getdate())

--Add 2 years
select DATEADD(Year, 2, getdate())
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/adding_dates_and_times_in_python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Distinct Rows Using Django Database Layer</title>
		<link>http://www.pressthered.com/getting_distinct_rows_using_django_database_layer/</link>
		<comments>http://www.pressthered.com/getting_distinct_rows_using_django_database_layer/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 05:20:45 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=572</guid>
		<description><![CDATA[To get the equivalent of the SQL select distinct statement in Django, you can use a combination of the values() and distinct() method of the QuerySet api.
For example, you have a musicians table and you want to get list of instruments that they play (with no duplicates):


Musicians.objects.values('instrument').distinct()

The values(&#8217;instrument&#8217;) method will generate a select statment of [...]]]></description>
			<content:encoded><![CDATA[<p>To get the equivalent of the SQL select distinct statement in Django, you can use a combination of the values() and distinct() method of the <a href="http://docs.djangoproject.com/en/dev/ref/models/querysets">QuerySet api</a>.</p>
<p>For example, you have a musicians table and you want to get list of instruments that they play (with no duplicates):</p>
<pre name="code" class="python">

Musicians.objects.values('instrument').distinct()
</pre>
<p>The values(&#8217;instrument&#8217;) method will generate a select statment of just the column instrument, while the distinct() method will apply the SELECT DISTINCT to the sql statement. The equivalent sql generated is: &#8220;select distinct instrument from musician&#8221;</p>
<p>One item to note (<a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct">mentioned in the documentation</a>), is when using the orderby() method to sort your data, the fields specified in the order by will also be included in the select statement. So you just have to be aware of it, especially if your results are not coming out as expected.</p>
<p>For example:</p>
<pre name="code" class="python">
# Possible gotcha
Musicians.objects.values('instrument').distinct().orderby('instrument_model_num')
</pre>
<p>The equivalent sql that is generated is &#8220;select distinct instrument, instrument_model_num from musician order by instrument_model_num&#8221;. This will not get you a list of just the instruments that the musicians play.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/getting_distinct_rows_using_django_database_layer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GUID Primary Keys in SQL</title>
		<link>http://www.pressthered.com/guid_primary_keys_in_sql/</link>
		<comments>http://www.pressthered.com/guid_primary_keys_in_sql/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 05:41:22 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[microsoft sql server]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=258</guid>
		<description><![CDATA[Creating a GUID in Microsoft SQL Server:

select newid();

Creating a GUID in MYSQL:

select uuid();

Why use a GUID as a primary key?
The main reason is if you want to guarantee uniqueness between databases and servers. 
I have never been a fan of GUIDs. Especially when having to write sql statements for reports. They&#8217;re difficult to scan and [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Creating a GUID in Microsoft SQL Server:</strong></p>
<pre name="code" class="sql">
select newid();
</pre>
<p><strong>Creating a GUID in MYSQL:</strong></p>
<pre name="code" class="sql">
select uuid();
</pre>
<p><strong>Why use a GUID as a primary key?</strong><br />
The main reason is if you want to guarantee uniqueness between databases and servers. </p>
<p>I have never been a fan of GUIDs. Especially when having to write sql statements for reports. They&#8217;re difficult to scan and impossible to sort. It used to be they were non existent and then they gained popularity when trying to store objects in relational databases became a fad. Try writing reports against an object hierarchy that is stored in a relational database. Every other column is a GUID. It&#8217;s not fun. </p>
<p>Anyway I had one recent experience that reinforced the need of a GUID in a database. Some production data was unintentionally lost in one table(due to a roll back). This particular table was using GUID&#8217;s as a primary key. The table could not be restored since it was already out of date. Instead the table was restored to a copy of the database. This allowed a quick comparison of the two tables and a painless insert via sql. What the GUID did in this case, was guarantee sameness (instead of uniqueness). </p>
<p>I am still going to stick to integers as primary keys but I am now going to add an auto generated GUID column for the sake of backup/restores/import/exports/etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/guid_primary_keys_in_sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
