求解,字符串如何取字段最大值再加1,返回值是字符串
有表comProduct , 字段 ProdId(字符串类型)
0021001
0021002
0021004
如何取到ProdId最大值再+1(也就是 0021005)
字符串转换 字符串最大值加1
[解决办法]
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-25 10:57:56
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
--Dec 28 2012 20:23:12
--Copyright (c) Microsoft Corporation
--Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[comProduct]
if object_id('[comProduct]') is not null drop table [comProduct]
go
create table [comProduct]([ProdId] varchar(7))
insert [comProduct]
select '0021001' union all
select '0021002' union all
select '0021004'
--------------开始查询--------------------------
select RIGHT('00000'+CONVERT(VARCHAR(10),MAX(CONVERT(INT,[ProdId]))+1),7)
from [comProduct]
----------------结果----------------------------
/*
--------------
0021005
*/
create table comProduct (ProdId nvarchar(10))
insert into comProduct values('0021001')
insert into comProduct values('0021002')
insert into comProduct values('0021004')
select RIGHT(CAST(POWER(10,7) AS VARCHAR)+MAX(CONVERT(INT,ProdId))+1,7)from comProduct
/*
ProdId
0021005
*/
create table comProduct (ProdId nvarchar(10))
insert into comProduct values('0021001')
insert into comProduct values('0021002')
insert into comProduct values('0021004')
SELECT RIGHT('0000000'+LTRIM(CAST(MAX(prodid) AS BIGINT)+1),7) FROM comProduct
/*
0021005
*/
--> 测试数据:[comProduct]
if object_id('[comProduct]') is not null drop table [comProduct]
go
create table [comProduct]([ProdId] varchar(20))
insert [comProduct]
select '0021001' union all
select '0021002' union all
select '0021004'
union all
select '00000021004'
--------------开始查询--------------------------
select RIGHT('00000000'+CONVERT(VARCHAR(10),MAX(CONVERT(INT,[ProdId]))+1),MAX(LEN([ProdId])))
from [comProduct]
----------------结果----------------------------
/*
------------------
00000021005
*/