Delete records older than 12 months but the date is in YYYYMM format

jennifer zen 341 Reputation points
2020-11-19T20:15:49.497+00:00

Hi,

I have a table that gets updated with fresh data every month
I am trying to delete records which are older than 12 months (1 year) from my table.(automate through SP to keep only rolling 12 months data), but the problem is the field 'LoadedDate' is in YYYYMM (201808)(varchar) format.(This field is actually the date to tell which month's data it is..... not like the date it was loaded in the table as the data insert is pretty random throughout the months)

Thanks in advance

Developer technologies | Transact-SQL
SQL Server | Other
{count} votes

Accepted answer
  1. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2020-11-19T22:34:59.42+00:00

    If I get this right:

    DELETE tbl
    WHERE LoadedDate < (SELECT convert(char(6), MAX(convert(int, LoadedDate)) - 100) FROM tbl

    That is, convert the highest value of LoadedDate to integer, deduct 100 to go back one year.

    (This is a little shortcut, and would not work well if the limit was 15 or 18 months.)

    1 person found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Nasreen Akter 10,811 Reputation points Volunteer Moderator
    2020-11-19T21:33:52.213+00:00

    Hi @jennifer zen ,

    I think you are looking for

    select CONVERT(varchar(6), DATEADD(month, -12, GETDATE()), 112)

    Here, GETDATE() will give you today's date and then DATEADD() will deduct 12 months from today's date and give you year and month as YYYYMM. You can use the result then your Delete Query.

    Hope this will help. Thanks!

    41222-yearmonth.jpg

    2 people found this answer helpful.

  2. Guoxiong 8,206 Reputation points
    2020-11-19T22:15:21.487+00:00
    DECLARE @MyTable TABLE (
        LoadedDate varchar(6)
    );
    INSERT INTO @MyTable VALUES
    ('201609'), ('201709'),('201809'), ('201909'), ('202009');
    
    DECLARE @numberOfMonthesDelayed int = 3;
    DECLARE @startMonthYearToBeDeleted varchar(20);
    SET @startMonthYearToBeDeleted = LEFT(CONVERT(varchar, DATEADD(month, -(12 + @numberOfMonthesDelayed), GetDate()), 112), 6);
    
    DELETE @MyTable WHERE LoadedDate <= @startMonthYearToBeDeleted;
    SELECT * FROM @MyTable;
    
    1 person found this answer helpful.
    0 comments No comments

  3. jennifer zen 341 Reputation points
    2020-11-19T22:46:44.097+00:00

    Hi All,

    All the answers worked :)

    Ass suggested by @Guoxiong , @NasreenAkter-2040, @SM

    I can use get date() and add three months delay as making it to -15 or as @Erland Sommarskog suggested..

    convert to integer and delete them ( as my limit is 12 months) ...

    As I couldn't mark them all as optional answers, have just upvoted your comment.

    Thanks a lot...appreciate your help :)

    1 person found this answer helpful.
    0 comments No comments

  4. Guoxiong 8,206 Reputation points
    2020-11-19T20:49:53.487+00:00
    DECLARE @MyTable TABLE (  
    	LoadedDate varchar(6)  
    );  
    INSERT INTO @MyTable VALUES  
    ('201609'), ('201709'),('201809'), ('201909'), ('202009');  
      
    DELETE @MyTable  
    WHERE LoadedDate <= '201808';  
      
    SELECT * FROM @MyTable;  
    

    41185-image.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.