merlot
merlot

Reputation: 620

Import empty string value to MySQL as date

I have a string array that I am importing to a MySQL table, containing strings but also date strings (e.g. '2017-01-01'). Some dates are zero ('') and MySQL does not recognise these as dates. Since I am using a string array I cannot use NULL.

How can I pass a zero date value to my table by using a string value? The result in the table should be null, not '00-00-0000' for example.

Thanks!

Upvotes: 0

Views: 210

Answers (2)

Erik Šťastný
Erik Šťastný

Reputation: 1486

Just add simple condition for check value of date

date = some_date_from_cvs

if date is not "":
    mysql.insert(date)
else:
    mysql.insert(NULL)

Upvotes: 1

Hussnain Raza
Hussnain Raza

Reputation: 1

According to my experience, you have to check if the string is empty then simple get the current Date using python script.

import time
## dd/mm/yyyy format
cur_date = (time.strftime("%d/%m/%Y"))
if str is in ['', None]:
    str = cur_date

After getting the date simply import to MYSQL.

Upvotes: 0

Related Questions