Operand type clash: date is incompatible with int

  • I have this query

    DECLARE @TheDate DATE

    IF @TheDate IS NULL

    SET @TheDate = GETDATE()

    SET @FileName = 'DailyAgentSales' + CONVERT(CHAR(8) ,@TheDate -1 ,112) + '.csv'

    SET @Subject = 'Agent Sales - ' + CONVERT(CHAR(8) ,@TheDate -1 ,112)

    And I'm getting this error: Operand type clash: date is incompatible with int,

    what it this error and where do I start to look at to solve it?

  • hoseam (3/15/2016)


    I have this query

    DECLARE @TheDate DATE

    IF @TheDate IS NULL

    SET @TheDate = GETDATE()

    SET @FileName = 'DailyAgentSales' + CONVERT(CHAR(8) ,@TheDate -1 ,112) + '.csv'

    SET @Subject = 'Agent Sales - ' + CONVERT(CHAR(8) ,@TheDate -1 ,112)

    And I'm getting this error: Operand type clash: date is incompatible with int,

    what it this error and where do I start to look at to solve it?

  • It's coming from this fragment: @TheDate -1

    You can't add or subtract integers from a DATE variable. Try DATEADD(dd,-1,@TheDate) in both places where that occurs

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thank you

  • You could also change this...

    DECLARE @TheDate DATE

    ... to this ...

    DECLARE @TheDate DATETIME

    ... and continue to do the -1 trick.

    Heh... now duck... here come the DATE/TIME police whose hair just burst into flames. 😛

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Eirikur Eiriksson (3/15/2016)


    hoseam (3/15/2016)


    I have this query

    DECLARE @TheDate DATE

    IF @TheDate IS NULL

    SET @TheDate = GETDATE()

    SET @FileName = 'DailyAgentSales' + CONVERT(CHAR(8) ,@TheDate -1 ,112) + '.csv'

    SET @Subject = 'Agent Sales - ' + CONVERT(CHAR(8) ,@TheDate -1 ,112)

    And I'm getting this error: Operand type clash: date is incompatible with int,

    what it this error and where do I start to look at to solve it?

    He he, tried to post the answer from a moving London Underground train, just noticed now that the actual answer never made it to the surface:-P

    😎

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply