SPSS, MS-SQL2008 & bigint
There seems to be an issue with SPSS while reading data from an MS-SQL-Server instance. Notably with the SQL datatype “bigint”. Assume the following SPSS syntax:
/TYPE=ODBC
/CONNECT='DSN=my_dsn;SERVER=server_name;Trusted_Connection=yes;DATABASE=db_name'
/SQL = 'SELECT year FROM my_table'
.
EXECUTE.
If the field in question (in this case: “year”) is of SQL-type “bigint” then SPSS will show these values in the majority of the cases as “MISSING”. Sporadically some values appear, but they are completely wrong.
Once the cause is known (problem with the “biging” type), the solution is straight-forward: Cast the type to another appropriate type which is understood by SPSS. Which type you choose obviously depends on the values stored in the affected fields. Casting blindly to “int” may (I haven’t tested this!) resolve in strange results if the values lie outside of the “int” range (-2^31 to 2^31-1). In this case you may need to cast it to something alphanumeric like “varchar” and re-cast it in SPSS into “Numeric”. As said, I haven’t tested this but I thought it might be worth mentioning!
So, here’s the above query with the appropriate cast:
/TYPE=ODBC
/CONNECT='DSN=my_dsn;SERVER=server_name;Trusted_Connection=yes;DATABASE=db_name'
/SQL = 'SELECT CONVERT(int, year) AS year FROM my_table'
.
EXECUTE.
Note also that in this case you need to add an alias for the column ( “… AS year” ). Otherwise SPSS will return it as “VXXX” (where XXX is a sequential number).
I have tested this solution on all combinations of SPSS 11.5, SPSS 18, SQL-Server 2008 64bit, SQL-Server 2008 Express 32bit. And casting the value worked every time.
Depending on your use, it may be helpful to create views which do the casting. I have not yet tried this, but I don’t see a reason why it shouldn’t work. Additionally, it might be noteworthy that I have only encountered this problem with “bigint” so far. There may be problems with other types as well. I expect, casting them to something else should work there too.
Posted in Coding Voodoo | No Comments »

