【问题标题】:How to convert an MS Access "IIF" query to a Sql Server query?如何将 MS Access“IIF”查询转换为 Sql Server 查询?
【发布时间】:2017-12-13 23:05:52
【问题描述】:
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

【问题讨论】:

标签: sql-server sql-server-2008 ms-access


【解决方案1】:

类似的东西。

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

【讨论】:

  • 第二个结果应该是 2,而不是 0。
【解决方案2】:

这个怎么样:

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;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    相关资源
    最近更新 更多