我有存储过程生成优惠券代码,在这里我要传递优惠券代码的前缀
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 to int转换失败,因为我试图解析'NEW001‘to int以找到最大值,是否可以将这个'NEW001’解析为int,忽略开头的字符?
发布于 2015-05-26 08:56:54
对于Sql Server,请尝试:
select @maxID = isnull(max(substring(CouponCode, patindex('%[0-9]%', CouponCode), len(CouponCode))),0) + 1
from Coupon where CouponCode Like @Prefix + '%'https://stackoverflow.com/questions/30453809
复制相似问题