简体   繁体   中英

Year number range formatting

I have these year number ranges

1993-1997
1923-1935
1998-2015

I'm trying to produce this shortened version of these year ranges.

1993-7
1923-35
1998-2015

So far my query looks like this. But its not working on the 2nd and 3rd samples, 1923-1935, and 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)

Note: Year is always in 4 digits.

I am assuming you want a single digit year if its within the same decade, and double digit year when its a different decade. In which case use a case statement to compare the decade component and then display the appropriate number of digits eg

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;

Assuming your input strings will always have same length ie 9 characters

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

There are actually 2 ways to address this.

Looking for the same from the right. Or looking for differences from the left.

Example snippet:

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;

A test snippet using a table variable:

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

A test on rextester here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM