Home > Enterprise >  Pandas read_sql_query with WHERE condition
Pandas read_sql_query with WHERE condition

Time:03-09

I am trying read data from SQL Server into a dataframe using pandas.read_sql_query. I have to set a condition where calendar year = 2020. I am using this syntax:

pd.read_sql_query('SELECT * FROM [abc] WHERE [calendarYear] = 2020', connectionObject)

I am getting an error:

Conversion failed when converting the nvarchar value 'UNKNOWN' to data type int.

Can anyone please help me with the syntax?

Thanks.

CodePudding user response:

This is probably because your calendarYear is a nvarchar so when you search only 2020 it throws an error. Try and put some '' around 2020 so your code would look like

pd.read_sql_query("SELECT * FROM [abc] WHERE [calendarYear] = '2020'", connectionObject)
  • Related