PRB: DATEADD Doesn't Work When Using a Variable as a Date

Last reviewed: April 25, 1997
Article ID: Q47048

The information in this article applies to:

  - Microsoft SQL Server version 4.2 for OS/2

SYMPTOMS

The use of a variable as a date in the DATEADD command can result in an error message of "Msg 242, Level 16, State 0:," stating that an out of range conversion was attempted.

WORKAROUND

This problem can be corrected by ensuring the variable is correctly declared as datetime.

MORE INFORMATION

Steps to Reproduce the Problem

  1. Issue the following query in ISQL:

          declare @valdate char(10)
          select @valdate=convert(datetime,'07/07/57')
          select dateadd(day,15,@valdate)
          go
    

  2. The following error message is returned:

          Msg 242, Level 16, State 0:
    

  3. The conversion of CHAR to DATETIME results in a DATETIME value out of range. Arithmetic overflow also occurs. However, for the given values, there should be no arithmetic overflow. In fact, the following query executes correctly:

          select dateadd(day,15,'07/07/57')
          go
    

This can be corrected by changing step 1 as follows:

      declare @valdate datetime
      select @valdate=convert(datetime,'07/07/57')
      select dateadd(day,15,@valdate)
      go


Additional query words: Transact-SQL
Keywords : kbtool SSrvBCP
Version : 4.2
Platform : OS/2


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 25, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.