## Python 3.3.5
from os import getenv
import pymssql
from datetime import datetime
host = 'tst-db'
user = 'qdb'
password = 'SuperHard'
database = 'QReports'
Data = datetime.now()
Time = datetime.now()
sysdate = ("{}-{}-{} {}:{}:{}.{}".format(Data.year, Data.month, Data.day, 00, 00, 00, 000))
systime = ("{}-{}-{} {}:{}:{}.{}".format(1900, 1, 1, Time.hour, Time.minute, Time.second, 000))
systime5 = ("{}-{}-{} {}:{}:{}.{}".format(1900, 1, 1, Time.hour, Time.minute, Time.second, 000))
sql = u"select Count(OrderNum) from Orders where TradeDate = convert(datetime, '{sysdate}', 120) and orderTime between convert(datetime, '{systime5}', 120) and convert(datetime, '{systime}', 120)"
#sql = u"select Count(OrderNum) from Orders where TradeDate = '2018-1-10 0:0:0.0' and orderTime between '1900-1-1 10:55:25.0' and '1900-1-1 10:55:25.0'"
conn = pymssql.connect(host, user, password, database)
cursor = conn.cursor()
cursor.execute(sql)
row = cursor.fetchone()
print(sysdate)
print(systime)
print(systime5)
print(row)
input()
Traceback (most recent call last):
File "pymssql.pyx", line 447, in pymssql.Cursor.execute (pymssql.c:7119)
File "_mssql.pyx", line 1011, in _mssql.MSSQLConnection.execute_query (_mssql.c:11586)
File "_mssql.pyx", line 1043, in _mssql.MSSQLConnection.execute_query (_mssql.c:11477)
File "_mssql.pyx", line 1241, in _mssql.MSSQLConnection.get_result (_mssql.c:13497)
File "_mssql.pyx", line 1588, in _mssql.check_cancel_and_raise (_mssql.c:16910)
File "_mssql.pyx", line 1630, in _mssql.maybe_raise_MSSQLDatabaseException (_mssql.c:17524)
_mssql.MSSQLDatabaseException: (241, b'Conversion failed when converting date and/or time from character string.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/November31st/PycharmProjects/DataExporter/DataChecker", line 23, in
cursor.execute(sql)
File "pymssql.pyx", line 467, in pymssql.Cursor.execute (pymssql.c:7561)
pymssql.OperationalError: (241, b'Conversion failed when converting date and/or time from character string.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n')
Process finished with exit code 1
sql = u"select Count(OrderNum) from Orders where TradeDate = '2018-1-10 0:0:0.0' and orderTime between '1900-1-1 10:55:25.0' and '1900-1-1 10:55:25.0'"
2018-1-10 0:0:0.0
1900-1-1 17:24:3.0
1900-1-1 17:24:3.0
(0,)