0

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!

3
  • 1
    I really dont understand where is problem. Both Mysql types DATE and DATETIME can be null so simple if date is "" then insert null into Mysql? Commented Mar 24, 2017 at 14:55
  • Sorry let me be more specific. I am creating a csv file from a string array, and then importing that CSV into MySQL. If the date is '' in the csv file, MySQL does not recognise this as a date and gives an error. Commented Mar 24, 2017 at 15:12
  • I mean exactly what i have said. Check my new answer. Commented Mar 27, 2017 at 10:10

2 Answers 2

1

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

Comments

0

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.

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.