0

I have values in few different variables and one of them stores date. When I checked data type in Python it shows Str. I added Convert(datetime,'{}',102) in my Insert statement to Insert this field in Database table since this field datatype in table is Date. This works fine when this variable has data.

Now, for some records this field is blank so Python variable holds None. How do I handle this? I want to insert NULL in this scenario.

Note-> I have this code inside for loop so I am inserting records one by one. This is why for some iterations variable might have actual date value and for some it might have None.

Current Code-> 
"Insert into table (col1,col2,col3) values ('{}',Convert(datetime,'{}',102),'{}')".format(var1,var2,var3)
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
PythonDeveloper
  • 289
  • 1
  • 4
  • 24
  • related: https://stackoverflow.com/a/61206056/2144390 – Gord Thompson Feb 09 '21 at 12:52
  • 1
    More related to [another question Gord has answered](https://stackoverflow.com/questions/60228932/update-statement-pyodbc-set-null-value-instead-of-none-in-sql-server). PyODBC will automatically convert None to Null in a parameterized query. Use Python to make sure any strings that do exist are in the correct format, then pass string-or-None as a plain parameter. – Alan Feb 09 '21 at 18:04

0 Answers0