Results 1 to 7 of 7

Thread: sql date time issue

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    sql date time issue

    yup

    right. Doing this project someone else has done is just complete.....lets not go there.

    When the user enters a date time in the format of:

    dd/mm/yyyy

    SQL will complain that it cannot convert char to datetime or something and that the value given exceeds the sql datetime limit. I am guessing it is expecting mm/dd/yyyy

    what is the best way to tackle this? Would I need to "switch" around the dd and mm?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: sql date time issue

    Why dont you try converting the format of the date and see if it helps.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: sql date time issue

    how do you mean? can you be more specific?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: sql date time issue

    Date objects have no format. Format is only an issue when representing a date as a string. If you don't do that then format is never an issue. If you're getting issues with format then it means you're using strings instead of binary date objects.

    How is the user entering this date? If it's a DateTimePicker, which should be the first choice, then there's never any need to even consider format in code. You simply get a DateTime object from its Value property and insert that into your database. If it's a TextBox then you need to convert the entered string to a DateTime object using Parse, TryParse or the like, then insert that DateTime object into the database. The database never sees anything but a binary date object so format is NEVER an issue.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: sql date time issue

    hmmm ok. Thanks ill try that. I dont know why all this matters with this project - its just shockingly done.

    the user is entering data in a textbox, and in the format of dd/mm/yyyy

    but if we enter say 12/06/2007, SQL will find records on the dateTime field of 06/12/2007 (dd/mm swapped around)

    ill try the DateTime.TryParse method but dont know how or why that will resolve the issue.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: sql date time issue

    As I have said, format only becomes an issue when representing dates as strings. If you put a string literal into an SQL statement then SQL Server will parse that string into a date value assuming that the string is in the format MM/dd/yyyy. If you use a DateTime object then there can be no issue because a binary date object has no format. It's just a value.

    Let's say that the user enters "12/06/2007" into a TextBox and you do this:
    C# Code:
    1. mySqlCommand.CommandText = "SELECT * FROM MyTable WHERE DateColumn = '" + myTextBox.Text + "'";
    That is a string, so the format of the date is an issue. SQL Server will ALWAYS parse that string to a date object using the form MM/dd/yyyy because SQL Server was created Americans. If you do this instead:
    C# Code:
    1. mySqlCommand.CommandText = "SELECT * FROM MyTable WHERE DateColumn = @DateColumn";
    2. mySqlCommand.Parameters.AddWithValue("@DateColumn", DateTime.ParseExact(myTextBox.Text, "dd/MM/yyyy", null);
    then you're specifying the format to use to parse the string to a binary date object, then you're passing that date object to SQL Server. The database doesn't have to think about format because a binary date object has no format. It's just a value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: sql date time issue

    thanks, thought about that and did it just before reading this

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

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