[英]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.但它不适用于第二个和第三个样本,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)
Note: Year is always in 4 digits.注意:年份始终为 4 位数字。
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在这种情况下,使用 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;
Assuming your input strings will always have same length ie 9 characters假设您的输入字符串将始终具有相同的长度,即 9 个字符
drop table if exists t
create table t (d varchar(9))
insert into t values
('1993-1997')
,('1923-1935')
,('1998-2015')
,('2095-2115')
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.