0

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.

1
  • Why is calendarYear not an int type? I strongly suggest you make it so Commented Mar 8, 2022 at 15:26

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

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.