【问题标题】:Calculate and Display Total Experience Months in YY/MM format in Sql Server在 Sql Server 中以 YY/MM 格式计算和显示总体验月
【发布时间】:2018-09-16 02:32:30
【问题描述】:

考虑下面的代码。

create table #temp (Min_Expr_InMonths int, Max_Expr_InMonths int)

insert into #temp values (40, 98)
insert into #temp values (null, null)
insert into #temp values (0, 0)
insert into #temp values (133, 145)

select ' (Exp - ' +  
    Convert(varchar, Case when j.Min_Expr_InMonths/12 < 10 then '0' + convert(varchar, j.Min_Expr_InMonths/12) else j.Min_Expr_InMonths/12 end) + '/' + 
    Convert(varchar, 
    case when j.Min_Expr_InMonths - ((j.Min_Expr_InMonths/12)*12) < 10 then '0' + convert(varchar, j.Min_Expr_InMonths - ((j.Min_Expr_InMonths/12)*12)) else
    j.Min_Expr_InMonths - ((j.Min_Expr_InMonths/12)*12) end) + ' to ' + Convert(varchar, 
    Case when j.Max_Expr_InMonths/12 < 10 then '0' + convert(varchar, j.Max_Expr_InMonths/12) else j.Max_Expr_InMonths/12 end) + '/' + 
    Convert(varchar, 
    case when j.Max_Expr_InMonths - ((j.Max_Expr_InMonths/12)*12) < 10 then '0' + convert(varchar, j.Max_Expr_InMonths - ((j.Max_Expr_InMonths/12)*12)) else
    j.Max_Expr_InMonths - ((j.Max_Expr_InMonths/12)*12) end) + ')' from #temp j

上述查询的输出如下。

YY/MM
(Exp - 3/4 to 8/2)
NULL
(Exp - 0/0 to 0/0)
(Exp - 11/1 to 12/1)

我想要达到的目标如下。

YY/MM
(Exp - 03/04 to 08/02)
(Exp - 00/00 to 00/00)
(Exp - 00/00 to 00/00)
(Exp - 11/01 to 12/01)

有没有更简单的方法来实现这一点,我的查询看起来很凌乱且难以阅读,而且随着数据库行的增加,我也不确定性能。

【问题讨论】:

    标签: sql performance sql-server-2008 formatting


    【解决方案1】:

    更正选择查询:

    SELECT '(Exp - ' +  
        CASE 
            WHEN j.Min_Expr_InMonths IS NULL 
                THEN '00' 
            WHEN (j.Min_Expr_InMonths/12 >= 10) 
                THEN CONVERT(VARCHAR, j.Min_Expr_InMonths/12) 
            ELSE 
                CONCAT('0',CONVERT(VARCHAR, j.Min_Expr_InMonths/12)) 
        END 
            + '/' + 
        CASE 
            WHEN j.Min_Expr_InMonths IS NULL 
                THEN '00' 
            WHEN j.Min_Expr_InMonths - ((j.Min_Expr_InMonths/12)*12) >= 10 
                THEN CONVERT(VARCHAR,j.Min_Expr_InMonths - ((j.Min_Expr_InMonths/12)*12)) 
            ELSE
                CONCAT('0',CONVERT(VARCHAR, j.Min_Expr_InMonths - ((j.Min_Expr_InMonths/12)*12))) 
        END 
            + ' to ' + 
        CASE 
            WHEN j.Max_Expr_InMonths IS NULL 
                THEN '00' 
            WHEN j.Max_Expr_InMonths/12 >= 10 
                THEN CONVERT(VARCHAR, j.Max_Expr_InMonths/12) 
            ELSE 
                CONCAT('0',j.Max_Expr_InMonths/12) 
        END 
            + '/' + 
        CASE 
            WHEN j.Max_Expr_InMonths IS NULL 
                THEN '00' 
            WHEN j.Max_Expr_InMonths - ((j.Max_Expr_InMonths/12)*12) >= 10 
                THEN CONVERT(VARCHAR, j.Max_Expr_InMonths - ((j.Max_Expr_InMonths/12)*12)) 
            ELSE
                CONCAT('0',CONVERT(VARCHAR,j.Max_Expr_InMonths - ((j.Max_Expr_InMonths/12)*12))) 
        END 
            + ')'
    FROM #temp j
    

    输出

    (Exp - 03/04 to 08/02)
    (Exp - 00/00 to 00/00)
    (Exp - 00/00 to 00/00)
    (Exp - 11/01 to 12/01)
    

    演示请点击链接:

    http://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=d462f3f8cc4b945e01a7d594b016a105

    【讨论】:

    • 非常感谢,它正在工作,当行数增加时会影响性能吗?
    【解决方案2】:

    请尝试以下最简单的查询:

    select 'Exp - ' + 
        right('00' + convert(varchar(2),coalesce(Min_Expr_InMonths,0)/12),2) + '/' +
        right('00' + convert(varchar(2),coalesce(Min_Expr_InMonths,0)%12),2) + ' to '+
        right('00' + convert(varchar(2),coalesce(Max_Expr_InMonths,0)/12),2) + '/' + 
        right('00' + convert(varchar(2),coalesce(Max_Expr_InMonths,0)%12),2) as 'YY/MM'
    from #temp
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 2021-10-02
      • 2012-09-26
      相关资源
      最近更新 更多