Sql server 如何转换MS Access“;国际投资论坛;查询到Sql Server查询? IIf((年([f_periodo])*12)+月([f_periodo])) -((年(日())*12)+月(日())

Sql server 如何转换MS Access“;国际投资论坛;查询到Sql Server查询? IIf((年([f_periodo])*12)+月([f_periodo])) -((年(日())*12)+月(日()),sql-server,sql-server-2008,ms-access,Sql Server,Sql Server 2008,Ms Access,类似这样的 IIf( ((Year([f_periodo])*12)+Month([f_periodo])) -((Year(Date())*12)+Month(Date()))<0,1,IIf( ((Year([f_periodo])*12)+Month([f_periodo])) -((Year(Date())*12)+Month(Date()))=0,2,3) )

类似这样的

IIf(  ((Year([f_periodo])*12)+Month([f_periodo]))
     -((Year(Date())*12)+Month(Date()))<0,1,IIf(  ((Year([f_periodo])*12)+Month([f_periodo]))
                                                 -((Year(Date())*12)+Month(Date()))=0,2,3)
    ) AS sts_exigible
选择案例
当(日期部分(年,[f_周期])*12+日期部分(月,[f_周期])-
(datepart(year,getdate())*12+datepart(month,getdate())<0然后1
当(日期部分(年,[f_周期])*12+日期部分(月,[f_周期])-
(datepart(year,getdate())*12+datepart(month,getdate())=0然后是2
其他3
以可生存的方式结束
这个怎么样:

select case
when (datepart(year, [f_periodo]) * 12 + datepart(month, [f_periodo])) - 
(datepart(year, getdate()) * 12 + datepart(month, getdate())) < 0 then 1
when (datepart(year, [f_periodo]) * 12 + datepart(month, [f_periodo])) - 
(datepart(year, getdate()) * 12 + datepart(month, getdate())) = 0 then 2
else 3
end as sts_exigible
试试这个:

sign(datediff(month, f_periodno, current_timestamp)) + 2;

我同意最好自己搜索答案。如果我不喜欢逆向工程和简化您的表达式,我可能会放弃花时间来帮助您。祝您好运。

SQL Server没有IIF,等效的是一个案例陈述:向我们展示您的尝试;这不是编程服务。@KevinRaffay不是tru然而,对于OP,您使用的是2008,所以请查找案例(Transact-SQL)。首先尝试自己解决问题,而不是自己不进行任何研究。简单的Google会为您指明正确的方向。其次,结果应该是2,而不是0。
declare @f_periodno datetime;

set @f_periodno = '20240101';
select sign(datediff(month, @f_periodno, current_timestamp)) + 2;

set @f_periodno = dateadd(day, -3, current_timestamp);
select sign(datediff(month, @f_periodno, current_timestamp)) + 2;

set @f_periodno = '20140101';
select sign(datediff(month, @f_periodno, current_timestamp)) + 2;