Menu

Howto insert datetime

Barlog M.
2009-02-19
2013-04-29
  • Barlog M.

    Barlog M. - 2009-02-19

    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.

     
    • bugfreeit

      bugfreeit - 2009-02-19

      what about using a literal
      http://msdn.microsoft.com/en-us/library/ms187819.aspx

      {ts '2009-02-19 17:47:33'}

       
    • A

      A - 2009-02-19

      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

       
    • Bjorn Pettersen

      Bjorn Pettersen - 2009-05-22

      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

       
      • A

        A - 2009-05-22

        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.

         
    • Ray Charles

      Ray Charles - 2009-07-20

      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

       
      • A

        A - 2009-07-20

        Read the whole thread, there are answers.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.