Tag Archive | "sql"

Adding Dates and Times in Python

Monday, October 19, 2009

2 Comments

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, […]

Continue reading...

Getting Distinct Rows Using Django Database Layer

Thursday, October 1, 2009

2 Comments

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(‘instrument’) method will generate a […]

Continue reading...

GUID Primary Keys in SQL

Thursday, June 18, 2009

Comments Off on GUID Primary Keys in SQL

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. […]

Continue reading...