Skip to content
Advertisement

How to insert values into the following table by incrementing the date column by 1 day for the next 23 days?

I am using SQL Server 2014. I have a table names T1 (extract shown below):

I need to update table T1 for period 2020-10-13 to 2020-11-04 with the following logic: All rows to be appended to the table will have the same values as those for ReviewDate on 2020-10-12 except the ReviewDate which will increment by 1 day until 2020-11-04 is reached.

So here is an extract of how the final table will look like (only appended ReviewDate 2020-10-13 shown:

NOTE: the table also contain entries with ReviewDate before 2020-10-12 but I just need to insert values into the table for a specific period with data related to 2020-10-12

How can I do that with a T-SQL query?

Advertisement

Answer

You need to insert rows. One method is to cross join to the dates you want and use that:

There are other ways to create the dates. One method is a recursive CTE:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement