如何自动将 1 年日期添加到 SQL Server 中的现有日期 [英] How automatically add 1 year date to an existing date in SQL Server
问题描述
我有一项任务是根据 DateCreated
列自动向 PatientsInfo
表中的所有注册患者收取 N2,500 的年度账单.
I have a task to automatically bill all registered patients in PatientsInfo
table an Annual Bill of N2,500 base on the DateCreated
column.
当然,我将使用存储过程将这些记录插入到PatientDebit
表中,并创建一个 SQL 作业来执行此过程.
Certainly I will use a stored procedure to insert these records into the PatientDebit
table and create a SQL Job to perform this procedure.
我将如何在 PatientsInfo
表中 select *
患者,其中 DateCreated
现在是 1 岁让我插入另一个表 PatientDebit
.
How will I select *
patients in PatientsInfo
table where DateCreated
is now 1 yr old for me to insert into another table PatientDebit
.
我的算法是这样的:
- 从
PatientsInfo
表中选择患者的注册日期 - 将 1 年添加到他们的
DateCreated
- 今天是否添加了日期?如果是,
- 将记录插入
PatientDebit
表中,帐单为N2,500 - 如果不是,什么都不做.
- select date of registration for patients from
PatientsInfo
table - Add 1 year to their
DateCreated
- Is date added today? if yes,
- Insert record into
PatientDebit
table with the bill of N2,500 - If no, do nothing.
请问我如何编写脚本?
推荐答案
使用 DATEADD,即:
Use DATEADD, i.e.:
SELECT DATEADD(year, 1, '2006-08-30')
参考:http://msdn.microsoft.com/en-us/library/ms186819.aspx
这篇关于如何自动将 1 年日期添加到 SQL Server 中的现有日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!