The sum of all SQL Server matter that exist,
and the space in which all SQL Server events
occur or could occur.
Welcome to SQL Server Universe.com Sign in | Join | Help
in
Home SS SLUG Forums Articles Photos Downloads

Adhoc way of SQL server support for datetime

Last post 02-27-2008, 13:06 by Dinesh Priyankara. 1 replies.
Sort Posts: Previous
  • Adhoc way of SQL server support for datetime

     02-27-2008, 12:11

    • Joined on 06-15-2007
    • Ganemulla
    • Posts 1
    • Points 0
    • Top 75 Contributor
      Male

    I am using SQL Server 2000.

    when I am querying data from following query it gives an error.

    Query: select * from SaleMain where dateinfo<='2008-02-25'
    and dateinfo>='2008-02-25'

    error: Server: Msg 242, Level 16, State 3, Line 1
    The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

    And occour the same error for : select * from SaleMain where dateinfo<=cast('2008-02-25' as datetime)
    and dateinfo>=cast('2008-02-25' as datetime)

    But when I am using this query it works:

    select * from SaleMain where dateinfo<=cast('2008-02-25 00:00:00.000' as datetime)
    and dateinfo>=cast('2008-02-24 00:00:00.000' as datetime)

    And then I try this query and works fine:  select * from SaleMain where dateinfo<=cast('2008-02-25' as datetime)
    and dateinfo>=cast('2008-02-25' as datetime)

    And also this query works after that: select * from SaleMain where dateinfo<='2008-02-25'
    and dateinfo>='2008-02-25'

    Actually I am amazed with this functionality. And I have experience in this type of adhoc behavior for datetime in earlier also.

    Can anyone please tell me, is there any sure and a very accurate way of writing these type of queries those are using datetime values.

     

  • Re: Adhoc way of SQL server support for datetime

     02-27-2008, 13:06

    • Joined on 11-22-2006
    • Colombo, Sri Lanka
    • Posts 102
    • Points 0
    • Top 10 Contributor
      Male
    Hi,

    You will get this type of errors when a different culture set in the regional setting that does not support the format you have written in the statement. Check your regional setting first and see.

    Another way of solving the issue is, using CONVERT function with style number (eg. CONVERT(datetime, '2008-02-25', style number). Try the CONVERT with style numbers 20 or 120 and see. For more info about this, read "CAST and CONVERT" in BOL.

View as RSS news feed in XML

(Best viewed with a resolution of more than 1024 * 768)

Powered by Community Server, by Telligent Systems