如何将MS Access“IIF”查询转换为Sql Server查询?

时间:2017-12-13 23:05:53

标签: 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)
    ) AS sts_exigible

2 个答案:

答案 0 :(得分:0)

像这样。

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

答案 1 :(得分:0)

这个怎么样:

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

试试这个:

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;

我同意最好自己寻找答案。如果我不喜欢逆向工程并简化你的表达,我可能会花时间去帮忙。祝你好运。