Results 1 to 7 of 7

Thread: [RESOLVED] SQL Server dates

  1. #1

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

    Resolved [RESOLVED] SQL Server dates

    Hello,

    in my web application, i insert use the getDate() function in sql to insert the dates into the database.
    They are stored as '2005/07/29 14:16:19'

    now i need to select records between 2 dates from the table.

    the format the user will select is DD/MM/YYYY

    what is the best way to convert this inside my sql statement so that sql server recognises it as yyyy/mm/dd

    or do i need to change it in my code?

    cheers
    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
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: SQL Server dates

    Quote Originally Posted by Strider
    Hello,

    in my web application, i insert use the getDate() function in sql to insert the dates into the database.
    They are stored as '2005/07/29 14:16:19'

    now i need to select records between 2 dates from the table.

    the format the user will select is DD/MM/YYYY

    what is the best way to convert this inside my sql statement so that sql server recognises it as yyyy/mm/dd

    or do i need to change it in my code?

    cheers
    Take a look at this Link regarding CAST and CONVERT
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  3. #3
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016

    Re: SQL Server dates

    SQL Server should handle this implicit conversion for you. I use the same thing here, getDate(), and it stores it in the same format. When my users search for records based on date, I don't do any type conversions and in fact, force them to use a MM/DD/YYYY format when they enter their dates.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  4. #4

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

    Re: SQL Server dates

    in my code i have my sql statement as
    "SELECT * FROM mytable WHERE DateReceived < Date.Today"

    where date.today will be 30/08/2005

    but if i use this i get the following error

    The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

    because the 2005 is not a day and is in the place where the day should be
    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

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SQL Server dates

    30/08/2005 is not a valid date for SQL Server, it should be 08/30/2005 instead (it always assumes American date format).

    If you can, convert the date to this format.

  6. #6

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

    Re: SQL Server dates

    god dharm americans!!!!!!!

    i decided to go with stored procedure and pass in a date 30/08/2005 to a datetime variable in the stored procedure and this done the conversion for me to the sql standard of 2005/08/30
    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

  7. #7
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: SQL Server dates

    Quote Originally Posted by Strider
    god dharm americans!!!!!!!
    But we still Love You
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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