从具有公式的日期开始减去6个月

时间:2017-08-29 22:06:55

标签: sql date ssms

我有一个公式替换日期时间字段中的一年,然后还将日期时间字段转换为日期:

cast(DATEADD(year,DATEDIFF(year,LAW_TAEEMASTER.MASTR_ENTRY,GETDATE()),LAW_TAEEMASTER.MASTR_ENTRY) as date) as [New Date]

返回如下结果:2017-08-07 from:2008-08-07。现在,我需要从中减去6个月,所以新的结果将是2017-02-07。我做了以下更改,导致错误

  

关键字'as'

附近的语法不正确
cast(DATEADD(Month,-6,cast(DATEADD(year,DATEDIFF(year,LAW_TAEEMASTER.MASTR_ENTRY,GETDATE()),LAW_TAEEMASTER.MASTR_ENTRY) as date) as [6 mo Anniv]

1 个答案:

答案 0 :(得分:2)

您忘记将括号括起来关闭新的CAST和DATEADD函数,以便将原始结果减去6个月。此外,您的新CAST功能应包括AS DATE。

<div id="div1"> <div class="box"> <span class="tooltiptext">c.carddata.description c.carddata.description c.carddata.description c.carddata.description c.carddata.description</span> <div class="text">c.carddata.name<br/>c.carddata.name</div> </div> <div class="box"> <span class="tooltiptext">c.carddata.description c.carddata.description c.carddata.description c.carddata.description c.carddata.description</span> <div class="text">c.carddata.name<br/>c.carddata.name</div> </div> <div class="box"> <span class="tooltiptext">c.carddata.description c.carddata.description c.carddata.description c.carddata.description c.carddata.description</span> <div class="text">c.carddata.name<br/>c.carddata.name</div> </div> <div class="box"> <span class="tooltiptext">c.carddata.description c.carddata.description c.carddata.description c.carddata.description c.carddata.description</span> <div class="text">c.carddata.name<br/>c.carddata.name</div> </div> <div class="box"> <span class="tooltiptext">c.carddata.description c.carddata.description c.carddata.description c.carddata.description c.carddata.description</span> <div class="text">c.carddata.name<br/>c.carddata.name</div> </div> <div class="box"> <span class="tooltiptext">c.carddata.description c.carddata.description c.carddata.description c.carddata.description c.carddata.description</span> <div class="text">c.carddata.name<br/>c.carddata.name</div> </div> <div class="box"> <span class="tooltiptext">c.carddata.description c.carddata.description c.carddata.description c.carddata.description c.carddata.description</span> <div class="text">c.carddata.name<br/>c.carddata.name</div> </div> </div>