0

I am trying to pull data from a certain database, the database is huge so I would like to query it before getting the data into python. I want to query on a specific column [Type] that I know is type varchar(50). As a test tried to query on an integer column successfully:

df = pd.read_sql_query('SELECT * FROM Prices where Price = 1', conn)

However when I try and do the same with the varchar column it tells me the column name is invalid.

df = pd.read_sql_query('SELECT * FROM Prices where Type = Daily', conn)

Thanks in advance!

AVad
  • 5
  • 2
  • `where Type = Daily` is comparing the Type column with the Daily column. Strings in SQL need to be quoted, just like any other language. Use `where Type = 'Daily'` – Panagiotis Kanavos Jun 23 '22 at 15:33
  • Im still getting an invalid column name with this :( – AVad Jun 23 '22 at 17:04
  • What is the *actual* error, what did you actually try? This isn't a matter of opinion. If you don't use quotes, it's not a string. Did you use the *correct* quotes? Did you use `"SELECT * FROM Prices where Type = 'Daily'"` ? If not you'd have to escape the single quotes in the Python string `'SELECT * FROM Prices where Type = \'Daily\''` – Panagiotis Kanavos Jun 24 '22 at 06:42
  • Try the query first with any client tool like SSMS or Azure Data Studio, which is actually a cross-platform SQL Server Data Studio. Once you have a valid query *then* convert it into a Python string – Panagiotis Kanavos Jun 24 '22 at 06:43

0 Answers0