【问题标题】:Confuse while generating coupon code生成优惠券代码时感到困惑
【发布时间】:2015-05-26 08:48:52
【问题描述】:

我有存储程序来生成优惠券代码,我在其中传递优惠券代码的前缀

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'解析为忽略起始字符​​p>

【问题讨论】:

  • 标记使用的 dbms! (看起来不像 ANSI SQL...)
  • 在我看来像 oracle 存储过程,但不确定
  • 我在 MS SQL 2008 中这样做

标签: sql sql-server-2008 stored-procedures


【解决方案1】:

对于Sql Server 试试:

select @maxID = isnull(max(substring(CouponCode, patindex('%[0-9]%', CouponCode), len(CouponCode))),0) + 1 
from Coupon where CouponCode Like @Prefix + '%'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多