34,590
社区成员
发帖
与我相关
我的任务
分享
declare @t table(col varchar(50))
insert @t select 'cgd-0802-10'
union all select 'cgd-0802-12'
union all select 'cgd-0802-13'
union all select 'cgd-0802-14'
select right(col,2) from @t
where col=(select max(col) from @t)
/*
----
14
*/
declare @t table(type varchar(13))
insert @t select 'cgd-0802-10'
insert @t select 'cgd-0802-12'
insert @t select 'cgd-0802-13'
insert @t select 'cgd-0802-14'
select max(substring(substring(type,charindex('-',type)+1,len(type)),charindex('-',substring(type,charindex('-',type)+1,len(type)))+1,len(type))) from @t
--
14
/*
type
14
*/
declare @t table(type varchar(13))
insert @t select 'cgd-0802-10'
insert @t select 'cgd-0802-12'
insert @t select 'cgd-0802-13'
insert @t select 'cgd-0802-14'
select max(reverse(left(reverse(type),charindex('-',reverse(type))-1))) as typae from @t
declare @t table(type varchar(13))
insert @t select 'cgd-0802-1'
insert @t select 'cgd-0802-2'
insert @t select 'cgd-0802-5'
insert @t select 'cgd-0802-14'
select PARSENAME(replace(type,'-','.'),1) as cnt from @t
select max(cast(cnt as int)) as maxcnt
from (select PARSENAME(replace(type,'-','.'),1) as cnt from @t) b
/*
cnt
--------------------------------------------------------------------------------------------------------------------------------
1
2
5
14
maxcnt
-----------
14
*/
create table tb(col varchar(20))
insert into tb values('cgd-0802-10')
insert into tb values('cgd-0802-12')
insert into tb values('cgd-0802-13')
insert into tb values('cgd-0802-14')
go
select 最大值 = max(cast(reverse(left(reverse(col),charindex('-',reverse(col)) - 1 ))as bigint)) from tb
drop table tb
/*
最大值
--------------------
14
(1 行受影响)
*/
create table #t(col varchar(200))
go
insert into #t select 'cgd-0802-5'
insert into #t select 'cgd-0802-12'
insert into #t select 'cgd-0802-13'
insert into #t select 'cgd-0802-14'
go
select max(cast(reverse(left(reverse(col),charindex('-',reverse(col))-1)) as int)) from #t
go
drop table #t
go
--结果
-----------
14
(1 行受影响)
create table tb(col varchar(20))
insert into tb values('cgd-0802-10')
insert into tb values('cgd-0802-12')
insert into tb values('cgd-0802-13')
insert into tb values('cgd-0802-14')
go
select 最大值 = max(reverse(left(reverse(col),charindex('-',reverse(col)) - 1 ))) from tb
drop table tb
/*
最大值
--------------------
14
(1 行受影响)
*/
--Value-字段,Table-表
select max(Right(Value, 2)) as iMax from Table
declare @t table(type varchar(13))
insert @t select 'cgd-0802-10'
insert @t select 'cgd-0802-12'
insert @t select 'cgd-0802-13'
insert @t select 'cgd-0802-14'
select PARSENAME(replace(type,'-','.'),1) as cnt from @t
select max(cnt) as maxcnt
from (select PARSENAME(replace(type,'-','.'),1) as cnt from @t) b
/*
cnt
--------------------------------------------------------------------------------------------------------------------------------
10
12
13
14
(所影响的行数为 4 行)
maxcnt
--------------------------------------------------------------------------------------------------------------------------------
14
(所影响的行数为 1 行)
*/