Results 1 to 15 of 15

Thread: [RESOLVED] SQL Date Issue

  1. #1

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Resolved [RESOLVED] SQL Date Issue

    Im having an issue when inserting a date into my sql server table.

    the table's date column is set to smalldatetime

    so when i try to insert the date 23/03/2005 into the column, when i look on sql server the field contains 01/01/1900

    however if i insert 03/23/2005, when i look on sql server the field contains 23/03/2005

    why is this happening..... my regional settings are set to GMT.
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  2. #2
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668

    Re: SQL Date Issue

    There's nothing wrong, thats just the way SQL stores dates.
    If wishes were fishes we'd all cast nets.

  3. #3

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: SQL Date Issue

    but why is it requiring me to insert the date in american format.

    why cant i insert as the european format

    when i insert as european date it gives me an error as it recognises that the 23 is not a month
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Date Issue

    Quote Originally Posted by Strider
    but why is it requiring me to insert the date in american format.

    why cant i insert as the european format

    when i insert as european date it gives me an error as it recognises that the 23 is not a month
    SQL Server is very date biased to the American format.

    We always use YYYY-MM-DD when doing date INSERT's - so that ambiguity does not become a problem.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: SQL Date Issue

    to make things all the more strange.......

    my application was working fine for the past two weeks then suddenly in the past few days its wont allow me to insert a date.now() into the date field....

    i get the error i previously mentioned....


    however, however...... when u insert date.today() it sets the date in the field to the 01/01/1900 ...and that aint the right date....

    what the hell is going on.
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Date Issue

    Quote Originally Posted by Strider
    to make things all the more strange.......

    my application was working fine for the past two weeks then suddenly in the past few days its wont allow me to insert a date.now() into the date field....

    i get the error i previously mentioned....


    however, however...... when u insert date.today() it sets the date in the field to the 01/01/1900 ...and that aint the right date....

    what the hell is going on.
    01/01/1900 is a "blank" date getting inserted into the date field.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: SQL Date Issue

    what do you mean by blank date????

    i just dont understand why date.today() gives me 01/01/1900 and date.now() gives me the error that there is no such month as the 23rd
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  8. #8
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668

    Re: SQL Date Issue

    Instead of letting using date.now() or whatever just use SQLs built in Now()

    ie.

    strSQL = "INSERT INTO TableX VALUES(" + id + ",'" + var1 + "',Now()," + var2 + ")"
    If wishes were fishes we'd all cast nets.

  9. #9

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: SQL Date Issue

    in my try catch i get the following error when using the Now() sql function

    'Now' is not a recognized function name

    here is my sql stmt

    Dim selectString As String = _
    "UPDATE tbl_UsersCompany SET " & _
    "Company='" & SafeSqlLiteral(p_customer.Company) & "', " & _
    "Address1='" & SafeSqlLiteral(p_customer.Addr1) & "', " & _
    "Address2='" & SafeSqlLiteral(p_customer.Addr2) & "', " & _
    "Address3='" & SafeSqlLiteral(p_customer.Addr3) & "', " & _
    "Town='" & SafeSqlLiteral(p_customer.Town) & "', " & _
    "RegionID= " & p_customer.Region & ", " & _
    "Phone='" & SafeSqlLiteral(p_customer.Phone) & "', " & _
    "Fax='" & SafeSqlLiteral(p_customer.Fax) & "', " & _
    "Email='" & SafeSqlLiteral(p_customer.Email) & "', " & _
    "URL='" & SafeSqlLiteral(p_customer.URL) & "'," & _
    "Active= " & l_active & ", " & _
    "SupportAgreement= " & l_suppAgreement & ", " & _
    "LastModified= Now(), " & _
    "LastModifiedUserId= 138" & _
    " WHERE UCID= " & p_customer.UCID '& "'"
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  10. #10
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668

    Re: SQL Date Issue

    hmm it could be Today()
    If wishes were fishes we'd all cast nets.

  11. #11

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: SQL Date Issue

    yup had a look there....its getDate() in sql and its works fine....coola peoples cheeers
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Date Issue

    Quote Originally Posted by Strider
    yup had a look there....its getDate() in sql and its works fine....coola peoples cheeers
    Just to further the point...

    GETDATE() is the SERVER time - not the workstation or PC time.

    It's always more appropriate, in my opinion, to use the server time and not the workstation time.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  13. #13

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: SQL Date Issue

    ya i think ill use that more often instead
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  14. #14
    New Member
    Join Date
    Jun 2005
    Posts
    1

    Re: SQL Date Issue

    Hi,

    I went throught your reply I have the same problem. Now in my case the system has been running for 4 months and this month on the 13th of june it started giving problems. When I checked the database (MS SQL) I found the date had reversed only for this month instead of saving as 6/1/2005 (June 1 2005) it started saving as 1/6/2005 (January 6 2005) and so when it came to 13 of this month the system gives an error message date out of range.

    When saving data I format it to yyyy/mm/dd as this format is international according to MSDN. I would appreciate if anyone can point out something that might help.

    Thanks

  15. #15

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: SQL Date Issue

    are you using the getdate() function because it will get the time from the sql server and insert that into the datetime field in the format of the regional settings on the server

    just make sure to do a check on your server that the regional settings are set to UK or Ireland and not US so that the date format is dd/mm/yyyy.
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

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