Python Forum
selecting from database using sql query by year
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
selecting from database using sql query by year
#1
Hi everybody,

I've got a question. I'm creating a query for a SQL database. Here is my query:

StartGesprek looks like '2018-07-11 15:54:40'
query="SELECT StartGesprek FROM dbo.tbl WHERE StartGesprek[:4]='2018'"

as you can see I want to select just the first 4 characters from StartGesprek but I get an error:
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near ':4'. (102)

I can not find anything on Google (or I'm not looking for the right thing.

I hope you bright boys and girls know the answer.
Reply
#2
StartGesprek[:4] is Python syntax, it's not SQL syntax. Everything in the query has to be SQL syntax. You would want something more like left(StartGesprek, 4).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Cheers, I will look into that.
Reply
#4
Is StartGesprek actually a varchar column, though? If it's a datetime2 (as it should be, if it's holding dates), then string manipulation won't work with it. But that's fine, because you have better options if it's a date.

Try this: where year(tbl.StartGesprek) = 2018
Reply
#5
I solved it by using:
if row[0].year==int(jaar)
.

Thanks for replying.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Query database menator01 0 740 Aug-29-2023, 08:14 AM
Last Post: menator01
  Query related to automation testing of database purnima1 0 2,214 Sep-29-2017, 08:27 AM
Last Post: purnima1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020