Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8839171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:10:50+00:00 2026-06-14T10:10:50+00:00

My database has alot of records recording the datetime various actions are or will

  • 0

My database has alot of records recording the datetime various actions are or will be taken. In order to test the database properly, i want to move all of those further into the past, by 6 months, a year etc (As opposed to changing the system time). How can i achieve this with a stored procedure, paramaterised by number of days?

(Bonus points if you also include other date/time types such as datetime2)

To be clear, i’m looking for something that will update all columns in all tables dynamically.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:10:51+00:00Added an answer on June 14, 2026 at 10:10 am

    Seems pretty simple…

    CREATE PROCEDURE dbo.MoveDatesBack @NumberOfDays INT
    AS BEGIN
    
       UPDATE dbo.YourTableNameHere
       SET SomeDateColum = DATEADD(DAY, @NumberOfDays, SomeDateColumn) 
    
       -- repeat the above statement for each table/column that you need to "move back"
    
    END
    

    Call this like so:

    EXEC  dbo.MoveDatesBack @NumberOfDays = -30
    

    or whatever you need …

    The DATEADD function should work with any date type in SQL Server 2008 or newer – if you apply it to a DATETIME2, you’ll get back a new DATETIME2

    But this seems too simple for Stackoverflow – what am I missing here??

    Update: if you want to do this globally, across all tables and all date-related columns in your entire database – you can use a cursor-based approach something like this:

    CREATE PROCEDURE dbo.MoveDatesBack @NumberOfDays INT
    AS BEGIN
      DECLARE TableCursor CURSOR FAST_FORWARD 
      FOR
         SELECT t.Name, c.name
         FROM sys.columns c 
         INNER JOIN sys.tables t ON c.object_id = t.object_id
         INNER JOIN sys.types typ ON c.system_type_id = typ.system_type_id
         WHERE typ.system_type_id IN (40, 42, 43, 61)  
         -- 40 = date, 42 = datetime2, 43 = datetimeoffset, 61 = datetime
    
      DECLARE @TableName sysname, @ColumnName sysname
    
      OPEN TableCursor
    
      FETCH NEXT FROM TableCursor INTO @TableName, @ColumnName
    
      WHILE @@FETCH_STATUS = 0
      BEGIN
          DECLARE @Stmt NVARCHAR(999)
    
          SET @Stmt = 'UPDATE ' + QUOTENAME(@TableName) + 
                      ' SET ' + QUOTENAME(@ColumnName) + ' = DATEADD(DAY, ' + 
                      CAST(@NumberOfDays AS VARCHAR(10)) + ', ' + 
                      QUOTENAME(@ColumnName) + ')'
    
          -- PRINT @Stmt
          EXEC (@Stmt)
    
          FETCH NEXT FROM TableCursor INTO @TableName, @ColumnName
      END
    
      CLOSE TableCursor
      DEALLOCATE TableCursor
    END
    

    Use at your own risk! This will do massive updates all across your database! Be sure to have ample backups at hand to roll back in case something goes wrong!!

    It’s pretty simplistic in that it assumes all tables are in the default schema – I don’t check and include schema information in this sample. Could be added – just make things a little bit more involved / more complicated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a database structure that will has good efficiency. Now database
I have a rather large database that has alot of decimal columns in alot
Database has tables Photos and PhotoAlbums. I need a query that will select all
My database has a table with thousands of records. The primary key is an
My database has records per SKU, giving product options (id's actually, but for clarity
I have a database which has an orders table and inventory table. The order-items
I have a large Oracle database ( 720,000 records aprox) where each record has
One of my database tables has a lot of records with a column containing
I'm writing some code that has to cascade delete records in a certain database,
I have inherited a database that has a lot of cursors in it, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.