Results 1 to 8 of 8

Thread: [RESOLVED] Milliseconds gettings stripped from SQL Server

  1. #1

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Resolved [RESOLVED] Milliseconds gettings stripped from SQL Server

    Gents,
    I am pulling a single field from a SQL Server 2008 table and when performing the SELECT in Management Studio, the result shows as:

    2011-08-03 17:46:36.997

    Now, in my application I'm storing that returned result as a string and using it to later perform an INSERT using it.

    I notice that after INSERTing into the database, I can see it is being stored as:

    2011-08-03 17:46:36.000

    Appears to be stripping off the milliseconds and replacing it with 000. In my case, milliseconds are important (sequencing thing) and can't figure out why it's doing that or how to fix it.

    Thoughts?

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Milliseconds gettings stripped from SQL Server

    How is the field in the database defined?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Milliseconds gettings stripped from SQL Server

    And let's see the code you are using to insert.

  4. #4

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Milliseconds gettings stripped from SQL Server

    It is a "datetime" field in the DB.

    Code for Insertion:

    So, first I get the time from the database:
    sActionTime = dsManitou.Tables(0).Rows(0).Item(2).ToString.Trim

    Then, later I use that "sActionTime" variable (a String) to use in my INSERT:

    vb.net Code:
    1. Private Sub InsertVideoLaunchedComment()
    2.         Dim con As New SqlConnection
    3.         Dim cmd As New SqlCommand
    4.         Dim sqlString As String = Nothing
    5.         Try
    6.             'GET THE NEXT AVAILABLE SEQUENCE NUMBER FOR THE SPECIFIC EVENT NUMBER
    7.             GetSequence()
    8.  
    9.             con.ConnectionString = "EDITED"
    10.             con.Open()
    11.             cmd.Connection = con
    12.             sqlString = "INSERT INTO CLOG" & Date.Now.ToString("yyyy") & Date.Now.ToString("MM") & " (LASTUPD, CREDATE, SERIALNO, LSEQNO, EVENTNO, EVDATE, LOGDATE, OFFSET, EVTYPE, QUAL, QUAL2, SIGQUAL, SOURCE, USRID, EVSTRING, SESSION, OWS) VALUES (GETUTCDATE(), GETUTCDATE(), '" & iSerial & "', '" & iSequence & "', '" & iEventNum & "', '" & sActionTime & "', GETUTCDATE(), '-300', '29', '0', '0', '0', 'X', '" & sOperator & "', 'ReView Video Verification was successfully launched', '" & iSession & "', '" & iOpWorkstation & "')"
    13.  
    14.             cmd.CommandText = sqlString
    15.             cmd.ExecuteNonQuery()
    16.         Catch ex As Exception
    17.             MyErrors &= Environment.NewLine & ex.ToString & Environment.NewLine
    18.         Finally
    19.             con.Close()
    20.         End Try
    21.     End Sub

    *i'm not using a parameterized query because no input comes from user*

    All the GETUTCDATE()'s return the correct date (including milliseconds), but for some reason the bolded "sActionTime" does not have the milliseconds on it.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Milliseconds gettings stripped from SQL Server

    This

    sActionTime = dsManitou.Tables(0).Rows(0).Item(2).ToString.Trim

    does not return milliseconds. The default for DateTime.ToString is M/d/yyyy h:mm:ss tt I think.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Milliseconds gettings stripped from SQL Server

    So, I just experimented with making it a DateTime variable and storing it that way, then using it in my query that way as well. It all still works, but same problem exists.

    I'll check into that dbas, that might be the problem (pretty silly if it is lol)

    EDIT: trying ToString("yyyy-MM-dd HH:mm:ss:fff") as we speak!

  7. #7
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Milliseconds gettings stripped from SQL Server

    This is why you should be using parameters. Forget user input, you can specify values and the data base types with the parameter if need be. There should never be a need for concatenated SQL statements, even if there is no user input. Not to mention your code will be much cleaner and more readable.

  8. #8

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Milliseconds gettings stripped from SQL Server

    I hear ya... and I won't lie, part of it is laziness. Parameters add so much more code that I just wasn't eager to type today (i'll go back and update them, not too hard)

    I changed it to the correct format before INSERTing and it is working now!

    I guess I probably should have checked that default format, but I assumed that taking whatever that value was and converting it straight to a string would maintain the entire thing, but it's odd that it stripped the milliseconds off.

    thanks fellas!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width