3

I am using Pyodbc to pull some data off our internal database, and am using the following SQL which works perfectly in SQL Server Management Studio.

'SELECT GrantInformation.GrantRefNumber, Count(GrantResearchTopic.PrimaryInd) AS CountOfPrimaryInd 
FROM GrantInformation 
LEFT JOIN GrantResearchTopic ON GrantInformation.GrantRefNumber = GrantResearchTopic.GrantRefNumber 
WHERE ((GrantResearchTopic.PrimaryInd)='True')  
GROUP BY GrantInformation.GrantRefNumber;'

For some reason if I remove the WHERE statement the SQL pull works, but if I leave it in, I get 'INVALID SYNTAX' despite as I said the SQL working fine in SQLSMS.

I have tried using double quotes around true, but get the following error:

ProgrammingError: ('42S22', "[42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'True'. (207) (SQLExecDirectW)")

Any ideas?

Edit: Problem solved. I didn't use double quotes on my SQL Statement so it wouldn't recognise the single quotes around the True statement. Using 1 worked as wasnt using quote marks, and using double quotes around the statement also worked as it then recognised the single quotes around 'True'. Thank you all.

4 Answers 4

4

Your whole SQL statement should be in double quotes not single quotes and then the true can remain in the single quotes.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that also worked. That was a simple mistake! Thank you Matt!
2

I solved a similar problem replacing 'True' with 1 (without quotes).

1 Comment

Perfect, yep, changing it to 1 fixed the problem. It is strange as I have done many SQL statements before with Pyodbc and never had this issue before, must be something about the database!
2

'True' (True enclosed by single or double quote) is a string and cannot be compared with a boolean. In sql using 1 or TRUE (both without quotes and TRUE is not case sensitive) for boolean true will work.

2 Comments

The 1 worked for me, the TRUE didn't, but either way, it was the fact I didnt double quote the SQL string which was causing the issue. thank you for your help! :)
Most probably your 'PrimaryInd' field is a bit, not a boolean.
-1

Try removing semicolon at the end of your statement

2 Comments

SQL statements can be semicolon terminated.
I tried removing it and still get the same syntax error pointing at the true statement :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.