Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion failed when converting date and/or time from character string. #148

Open
kevin-olbrich opened this issue Jun 3, 2017 · 2 comments

Comments

@kevin-olbrich
Copy link

kevin-olbrich commented Jun 3, 2017

I am unable to filter on a datetime colum (u_sentinstant):

Traceback (most recent call last):
  File "/usr/lib/python3.5/site-packages/django_pyodbc/base.py", line 489, in execute
    return self.cursor.execute(sql, params)
pyodbc.DataError: ('22007', '[22007] [FreeTDS][SQL Server]Conversion failed when converting date and/or time from character string. (241) (SQLExecDirectW)')

Code:

class Command(BaseCommand):
    help = 'Sends email notifications'

    def handle(self, *args, **options):
        now = datetime.now()

        all_open_port = EmailTable.objects.using('prod').filter(u_sentinstant__gte = datetime.today())

        for p in all_open_port:
            print(p.u_sentinstant)

        self.stdout.write(self.style.SUCCESS('Successfully finished.'))

Tested on Fedora 25 using FreeTDS (TDS_Version 7.2 and 7.3) and unixODBC and Windows 10. Backend is SQL SRV 2014.

all_open_port.query:

SELECT `emailtable`.`Code`, `emailtable`.`Name`, `emailtable`.`U_portjobid`, `emailtable`.`U_notifrtype`, `emailtable`.`U_address`, `emailtable`.`U_sentinstant`, `emailtable`.`U_sentreminder`, `emailtable`.`U_sentportdone` FROM `emailtable` WHERE `emailtable`.`U_sentinstant` >= 2017-06-03 14:24:30.137877

It would work if milliseconds are removed and the string is in single quotes.

@kevin-olbrich
Copy link
Author

Seems like...
https://github.com/michiya/django-pyodbc-azure
... has better support for latest Django and SQL 2014 / 2016.

@dlo dlo added the help wanted label Feb 1, 2018
@dlo dlo added the bug label Mar 7, 2018
@variable
Copy link

I had the exact error but was caused by date format, I re-arranged the dateformat from 'dd/mm/yyyy' to 'yyyy-mm-dd' has fixed the problem. https://stackoverflow.com/questions/14119133/conversion-failed-when-converting-date-and-or-time-from-character-string-while-i

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants