我有一列接受 NoOfMonths 中的值,我想以年为单位进行计算。我的表结构让我们说如下

CREATE TABLE Duration
(
TotalMonths int
);

INSERT INTO Duration VALUES (24);
INSERT INTO Duration VALUES (18);
INSERT INTO Duration VALUES (7);

我希望结果像这样显示,
Months  Duration
24      2 Years
18      1 Year 6 Months
7       7 Months

最佳答案

试试这个

SELECT
    *,
    CASE WHEN TotalMonths>12
          THEN CAST(TotalMonths/12 AS VARCHAR(10))+' Years '
       ELSE '' END
    +
    CASE WHEN TotalMonths%12>0
          THEN CAST(TotalMonths%12 AS VARCHAR(10))+' Months'
       ELSE '' END
    FROM Duration

关于sql-server - 如果月份从 12 个月增加,则计算以年为单位的持续时间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48399004/

10-12 17:23