我有存储程序以生成优惠券代码,其中我通过了优惠券代码的前缀
CREATE PROCEDURE [dbo].[spCouponCode1]
@Prefix varchar(50),
@Lenght varchar(50)
AS
Begin
declare @maxID as bigint=0
declare @PrefixLenght as bigint=0
set @PrefixLenght=LEN(@Prefix)
select @maxID = isnull(max(substring(CouponCode,@PrefixLenght+1,@PrefixLenght+1+@Lenght)),0) + 1 from Coupon where CouponCode Like @Prefix + '%'
select @Prefix + cast(@maxID as VARchar(100))
end
它完美无缺 问题是 如果在优惠券表中有优惠券代码,如'FIRST0001'和'FIRSTNEW001' 这里我得到错误 - varchar到int转换失败,因为我试图解析'NEW001'到int找到最大值 是否可以将此'NEW001'解析为int忽略起始字符 p>
答案 0 :(得分:2)
Sql Server
尝试:
select @maxID = isnull(max(substring(CouponCode, patindex('%[0-9]%', CouponCode), len(CouponCode))),0) + 1
from Coupon where CouponCode Like @Prefix + '%'