Howto insert datetime?
I'm try insert string in need format and convert, or cast, but get error:
SQL Server message 242, severity 16, state 3, line 2:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
DB-Lib error message 242, severity 16:
General SQL Server error: Check messages from the SQL Server
When i'm execute some query from SQL Server Management Studio, it's work without errors.
insert into test (Date)
values (convert(datetime, '19.02.2009 17:47:33'))
I work in linux, freetds-0.82.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sorry about re-opening an old thread... but I was wondering if there was a list of placeholders somewhere. I just tried the following to insert a datetime, table def.:
create table dktest42 (n int, d datetime)
and from python/pymssql (I guessed that the %s is a catch-all...):
c.execute('insert into dktest42 (n,d) values (%d, %s)', (42, datetime.now()))
and it worked flawlessly (Windows/SQLServer 2000).
Is this the right way to do this?
-- bjorn
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is the right way. You can indeed thing of %s as a catch-all. The values from tuple get quoted based on their data types, but their final representation is a string, so yes, %s is enough for everything.
I'll document that soon. Thanks for pointing that out.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Howto insert datetime?
I'm try insert string in need format and convert, or cast, but get error:
SQL Server message 242, severity 16, state 3, line 2:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
DB-Lib error message 242, severity 16:
General SQL Server error: Check messages from the SQL Server
When i'm execute some query from SQL Server Management Studio, it's work without errors.
insert into test (Date)
values (convert(datetime, '19.02.2009 17:47:33'))
I work in linux, freetds-0.82.
what about using a literal
http://msdn.microsoft.com/en-us/library/ms187819.aspx
{ts '2009-02-19 17:47:33'}
Please use this instead
insert into test (Date)
values (convert(datetime, '19.02.2009 17:47:33', 104))
and consult this webpage for details
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Sorry about re-opening an old thread... but I was wondering if there was a list of placeholders somewhere. I just tried the following to insert a datetime, table def.:
create table dktest42 (n int, d datetime)
and from python/pymssql (I guessed that the %s is a catch-all...):
c.execute('insert into dktest42 (n,d) values (%d, %s)', (42, datetime.now()))
and it worked flawlessly (Windows/SQLServer 2000).
Is this the right way to do this?
-- bjorn
This is the right way. You can indeed thing of %s as a catch-all. The values from tuple get quoted based on their data types, but their final representation is a string, so yes, %s is enough for everything.
I'll document that soon. Thanks for pointing that out.
Hello,
I am facing a similar problem inserting the current datetime into an sql database.
my statement :
cur.executemany("INSERT INTO AuditTable VALUES(%d,"date goes in here",%d )", [ (12,"date goes in here" ,88 )])
I cannot find the right place holder. like %d for integer is there something to hold the date?
thanks
Read the whole thread, there are answers.