年份数字范围格式

Year number range formatting

我有这些年份的数字范围

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 位数字。

我假设如果它在同一个十年内,你想要一个单位数的年份,如果它在不同的十年内,你想要两位数的年份。在这种情况下,使用 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;

假设您的输入字符串始终具有相同的长度,即 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

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

从右边找同样的。 或者从左边寻找差异。

示例片段:

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;

使用 table 变量的测试片段:

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;

Returns:

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