我已经在nvarchar列中填充了一些字符串值。字符串的格式如下:
例如:16B,23G,128F,128M等…
我需要从中找出最大值,然后从代码中生成下一个。提取最大物品的逻辑如下:
例如,上述系列中最大的字符串是128M。
现在,我需要生成下一个序列。下一个字符串将有
任何人都可以让我知道哪种SQL可以为我提供所需的字符串。
假设:
CREATE TABLE MyTable ([Value] varchar(4)) ; INSERT INTO MyTable ([Value]) VALUES ('16B'), ('23G'), ('128F'), ('128M') ;
你可以做:
select top 1 case when SequenceChar = 'Z' then cast((SequenceNum + 1) as varchar) + 'A' else cast(SequenceNum as varchar) + char(ascii(SequenceChar) + 1) end as NextSequence from ( select Value, cast(substring(Value, 1, CharIndex - 1) as int) as SequenceNum, substring(Value, CharIndex, len(Value)) as SequenceChar from ( select Value, patindex('%[A-Z]%', Value) as CharIndex from MyTable ) a ) b order by SequenceNum desc, SequenceChar desc
sqlfiddle