【问题标题】:Year number range formatting年份数字范围格式
【发布时间】:2019-12-12 13:59:34
【问题描述】:

我有这些年份的数字范围

1993-1997
1923-1935
1998-2015

我正在尝试制作这些年份范围的缩短版本。

1993-7
1923-35
1998-2015

到目前为止,我的查询看起来像这样。但它不适用于 1923-1935 和 1998-2015 的第二个和第三个样本。

declare @bookyear varchar(50)
declare @year1 char(4)
declare @year2 char(4)

set @bookyear = '1993-1997'

set @year1 = substring(@bookyear, 1, charindex('-', @bookyear)-1)
set @year2 = substring(@bookyear, charindex('-', @bookyear) + 1, len(@bookyear))

select  cast(@year1 as varchar(50)) + '-'+ substring(@year2, 4, 1)

注意:年份始终为 4 位数字。

【问题讨论】:

    标签: sql sql-server substring charindex


    【解决方案1】:

    我假设你想要一个数字年份,如果它在同一个十年内,而你想要一个两位数的年份,如果它在不同的十年内。在这种情况下,使用 case 语句来比较十进制分量,然后显示适当的位数,例如

    declare @bookyear varchar(50), @year1 char(4), @year2 char(4);
    
    set @bookyear = '1993-1997';
    
    set @year1 = substring(@bookyear, 1, charindex('-', @bookyear)-1);
    set @year2 = substring(@bookyear, charindex('-', @bookyear) + 1, len(@bookyear));
    
    select cast(@year1 as varchar(50)) + '-'
      + case when substring(@Year1,1,3) = substring(@Year2,1,3) then substring(@year2, 4, 1)
        when substring(@Year1,1,2) = substring(@Year2,1,2) then substring(@year2, 3, 2)
        else substring(@year2, 1, 4) end;
    

    【讨论】:

      【解决方案2】:

      假设您的输入字符串将始终具有相同的长度,即 9 个字符

      drop table if exists t
      create table t (d varchar(9))
      insert into t values
      ('1993-1997')
      ,('1923-1935')
      ,('1998-2015')
      ,('2095-2115')
      

      SQLFIDDLE

      select d
          , case 
                  when LEFT(d, 3) =  LEFT(RIGHT(d , 4), 3) then LEFT(d, 5) + RIGHT(d, 1) 
                  when LEFT(d, 2) =  LEFT(RIGHT(d , 4), 2) then LEFT(d, 5) + RIGHT(d, 2) 
                  when LEFT(d, 1) =  LEFT(RIGHT(d , 4), 1) then LEFT(d, 5) + RIGHT(d, 3) 
                  ELSE d
          end
      FROM t
      

      【讨论】:

        【解决方案3】:

        实际上有两种方法可以解决这个问题。

        从右边寻找相同的东西。 或者从左边寻找差异。

        示例 sn-p:

        declare @bookyear varchar(9);
        set @bookyear = '1993-1997';
        
        --
        --  looking for different digits from left to right
        --
        set @bookyear = left(@bookyear,5) + 
              case
              when left(@bookyear,1) != substring(@bookyear,6,1) then right(@bookyear,4) 
              when left(@bookyear,2) != substring(@bookyear,6,2) then right(@bookyear,3)
              when left(@bookyear,3) != substring(@bookyear,6,3) then  right(@bookyear,2) 
              else right(@bookyear,1)
              end;
        
        select @bookyear as bookyear1;
        
        -- reset variable
        set @bookyear = '1993-1997';
        
        --
        --  looking for same digits from right to left
        --
        set @bookyear = left(@bookyear,5) + 
              case
              when left(@bookyear,3) = substring(@bookyear,6,3) then substring(@bookyear,9,1) 
              when left(@bookyear,2) = substring(@bookyear,6,2) then substring(@bookyear,8,2) 
              when left(@bookyear,1) = substring(@bookyear,6,1) then substring(@bookyear,7,3) 
              else substring(@bookyear,6,4) 
              end;
        
        select @bookyear as bookyear2;
        

        使用表变量的测试 sn-p:

        declare @Test table (bookyear varchar(9));
        insert into @Test (bookyear) values
        ('1993-1997'),
        ('1923-1935'),
        ('1998-2015'),
        ('2095-2115');
        
        select 
        bookyear,
        left(bookyear,5) + 
          case
          when left(bookyear,1) != substring(bookyear,6,1) then right(bookyear,4) 
          when left(bookyear,2) != substring(bookyear,6,2) then right(bookyear,3)
          when left(bookyear,3) != substring(bookyear,6,3) then right(bookyear,2) 
          else right(bookyear,1)
          end as bookyear1,
        left(bookyear,5) + 
          case
          when left(bookyear,3) = substring(bookyear,6,3) then substring(bookyear,9,1) 
          when left(bookyear,2) = substring(bookyear,6,2) then substring(bookyear,8,2) 
          when left(bookyear,1) = substring(bookyear,6,1) then substring(bookyear,7,3) 
          else substring(bookyear,6,4) 
          end as bookyear2
        from @Test;
        

        返回:

        bookyear    bookyear1   bookyear2
        1993-1997   1993-7      1993-7
        1923-1935   1923-35     1923-35
        1998-2015   1998-2015   1998-2015
        2095-2115   2095-115    2095-115
        

        对 rextester 的测试here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-05
          • 2019-01-13
          • 2021-09-23
          • 2018-06-09
          相关资源
          最近更新 更多