如何在varchar中获取max no并增加1 [英] How to get the max no in varchar and increment by 1

查看:72
本文介绍了如何在varchar中获取max no并增加1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQL Server数据库中,我有一个数据类型为varchar(20)的字段。

每个带有前缀EMB的数据插入。假设我在该领域有三个数据。这些是

MBA00001

MBA00002

MBA004042

MBA00500



这里最大数字是MBA004042,但查询

选择max(TraRentAsset),因为RentInfo的TraRentAsset带来了MBA00500。

如何定义MBA004042并增加1结果将是MBA004043。

请帮帮我。我将非常感谢你。

In SQL Server database I there is a field which datatype is varchar(20).
Every data insert with a prefix EMB. Suppose I have three data in the field. These are
MBA00001
MBA00002
MBA004042
MBA00500

Here the max number is MBA004042 but the query
select max(TraRentAsset) as TraRentAsset from RentInfo brings MBA00500.
How can I define the MBA004042 and increment 1. The result will be now MBA004043.
Please help me. I will be very grateful to you.

推荐答案

select top 1 TraRentAsset from RentInfo
order by cast(SUBSTRING(TraRentAsset, 4, LEN(TraRentAsset)) as int) desc





DEMO [ ^ ]


使用它..

Use this..
select max(substring(isnull(TraRentAsset,'MBA0'),4,len(isnull(TraRentAsset ,'MBA0'))))+1 


检查这个



check this

Create table #file(col1 varchar(20))
insert into #file select 'MBA00001'
insert into #file select 'MBA00002'
insert into #file select 'MBA004042'
insert into #file select 'MBA00500'

select  'MBA'+Convert(Varchar(20),MAX(convert(int,REPLACE(col1,'MBA','')))) from #file


这篇关于如何在varchar中获取max no并增加1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆