How to automatically add a 1 year date to an existing date in SQL Server

I have a task to automatically invoice all registered patients in the table PatientsInfoas an annual bill in the amount of 2500 manat in a column DateCreated.

Of course, I will use the stored procedure to insert these records into the table PatientDebitand create an SQL job to complete this procedure.

How will I be select *patients in the table PatientsInfo, where DateCreatednow 1 year for me to insert into another table PatientDebit.

I have my algorithm:

  • select the registration date for patients from the table PatientsInfo
  • Add 1 year to their DateCreated
  • Is date added today? If yes,
  • Insert an entry into the table PatientDebitwith account N2,500
  • If not, do nothing.

How do I write a script?

+5
source share
3 answers

Use DATEADD, i.e.:

SELECT DATEADD(year, 1, '2006-08-30')

Ref .: http://msdn.microsoft.com/en-us/library/ms186819.aspx

+12
source

Assuming the columns from the two tables are the same:

INSERT INTO PatientDebit
SELECT * from PatientsInfo WHERE DateCreated<DATEADD(year, -1, GETDATE())

Make sure you have an index on DateCreatedif it PatientsInfohas many entries, as it could potentially be slow otherwise

+6
source

sql .add addyear(). .add(, , ). sql datetime , . . #.

DATEADD (). . GETDATE().

0

All Articles