1

I am using Pyodbc to access a Microsoft SQL Server database. I have the following query being built in code:

SELECT created, transactionID, balanceEffect
FROM dbo.table
WHERE (SELECT YEAR(created) = 2020) 
 AND (SELECT MONTH(created) = 11) 
 AND (SELECT DAY(created) = 17) 
 AND balanceEffect > 0 
 AND TransactionTypeID = '1321837129837129213' 
ORDER BY created

Where created is a DATE column.

When it gets to executing the query I see the following error:

ProgrammingError: ('42000', "[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near '='. (102) (SQLExecDirectW); [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near '='. (102); [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near '='. (102)")

It seems like there is a problem with the subquery stuff. I haven't been able to find a similar issue online, and I believe this code (or a very similar piece of code) was working previously in this project. Does anyone have any idea what might be causing it?

marc_s
760k186 gold badges1.4k silver badges1.5k bronze badges
asked Nov 17, 2020 at 2:33
1
  • You don't need the subqueries. Just WHERE YEAR(created) == 2020 AND ... Commented Nov 17, 2020 at 2:34

2 Answers 2

0

The SELECTs are not correct. You just want:

WHERE YEAR(created) = 2020 AND 
 MONTH(created) = 11
 DAY(created) = 17 AND
 balanceEffect > 0 AND
 TransactionTypeID = '1321837129837129213' 

Or more simply:

WHERE CAST(created as DATE) = '2020-11-17' AND
 balanceEffect > 0 AND
 TransactionTypeID = '1321837129837129213' 
answered Nov 17, 2020 at 2:40
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for your reply. This is what I needed, much appreciated. Don't know why I thought I needed to select, fresh pair of eyes saved the day cheers
0

The subqueries make no sense, and are not needed. Just check created against literal dates, like this:

SELECT created, transactionID, balanceEffect
FROM dbo.table
WHERE 
 created >= '20201107' and created < '20201108'
 AND balanceEffect > 0 
 AND TransactionTypeID = '1321837129837129213' 
ORDER BY created

You might want to simplify the date filtering conditions as:

CONVERT(date, created) = '20201107'

However this is less efficient: the whole created column needs to be converted to a date before it can be filtered; this cannot take advantage of an index. This is why I recommend the half-open interval filtering, as shown in the first query.

Of course, if created has no time component, then you would just do:

created = '20201107'
answered Nov 17, 2020 at 2:40

1 Comment

Thank you for your reply very helpful info. Oversights by me

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.