Data Older than 12 month

  • I have date which is stored in Integer how do I get data older than 12 months

    select * from table1

    where data = 20190131 (This is how the date is stored in table as Integer)

  • Something like this, which isn't very efficient.  Better to stored your dates as dates if that's an option.

    WHERE DATEADD(MONTH,12,CAST(CAST(data AS char(8)) AS date)) < GETDATE()

    John

  • Thanks I am able to get my result.

    This is how the application has setup date unfortunately

     

    Thanks Once again

     

  • You can potentially shortcut John's method. If you add or subtract 10000 to an INT date held as YYYYMMDD, you are effectively adding or subtracting a year.

    (If one of your dates is 29 Feb, this method does not always work.)

     

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • John Mitchell-245523 wrote:

    Something like this, which isn't very efficient.  Better to stored your dates as dates if that's an option.

    WHERE DATEADD(MONTH,12,CAST(CAST(data AS char(8)) AS date)) < GETDATE()

    John

    John, I think it would be faster to convert GETDATE() rather than the data. Something like this

    DECLARE @D1 INT = CAST(CONVERT(VARCHAR(10), DATEADD(YEAR, -1, GETDATE()), 112) AS INT)

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Yes, that would be better.  Thanks Phil.

    John

  • This was removed by the editor as SPAM

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

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