User Tools

Site Tools


mssql

Cheat sheet for MSSQL 2008

Get table names from database

SELECT TABLE_NAME FROM information_schema.tables

Show table structure

EXEC sp_columns <tablename>

Limit output to x lines

SELECT top <x> * FROM <tablename

Dates and times

Date conversion

The magic number in the convert function determines the datestyle: 120 == ODBC canonical.

More about converting http://msdn.microsoft.com/en-us/library/aa226054%28v=sql.80%29.aspx][here

Select events that are active at a certain time:

SELECT event, starttime, endtime
FROM TABLE
WHERE  CONVERT(datetime,'2012-08-04 12:00:00',120) BETWEEN starttime AND endtime

Python and mssql

Caveats

Error message: “Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier.”

can be resolved by adding tds_version = 8.0 to your odbc.ini

(the Why is nicely summarised e.g. by Ubuntitis http://www.ubuntitis.com/?p=64)

mssql.txt · Last modified: 2012/09/26 14:02 by mantis