您好,欢迎访问一九零五行业门户网

sqlsever t-sql判断输入的数字重复出现的次数

文章介绍了利用sql存储加while一个个判断,然后再利用case when then else end 多条件判断 以指定的次数重复字符。
利用一个存储过程
代码如下 复制代码
alter proc numshowtimes
(
@numvalue nvarchar(max)
)
as
begin
    declare @temptable table(num nvarchar(4))---定义存放数字的表
    declare @tempnum nvarchar(4) --
    declare @value nvarchar(max)
    set @value = @numvalue
    ---判断输入的是否是数字
    while(len(@numvalue)>0)
        begin
            set @tempnum=substring(@numvalue,len(@numvalue),1)
            set @numvalue=substring(@numvalue,1,(len(@numvalue)-1))
            if @tempnum like'[^0-9]'
                begin
                    print 'you input not num'
                    return
                end
        end
    ----获取数据插入表中
    while(len(@value)>0)
        begin
            set @tempnum=substring(@value,len(@value),1)
            set @value=substring(@value,1,(len(@value)-1))
            insert into @temptable values (@tempnum)
        end
    select num,count(num)as [count] from @temptable group by num
end
去掉判断输入的是否是数字那个while循环,就显示所输入的所字符出现的次数,包括汉字
sql利用case when then else end 多条件判断 以指定的次数重复字符 (生成当天最大顺序编号)
代码如下 复制代码
select
case
    when a is not null then a
    when b is not null then b
    when c is not null then c
    when d is not null then d
    else ''
end 列名
from table name
sql利用case when then多条件判断
 代码如下 复制代码
select top 100 state,joinstate,
(case
when state=1 and joinstate=0 then 2
when state=1 and joinstate=1 then 1
else 0
end )
as usestate
from userinfo
(生成当天最大顺序编号)
substring(convert(varchar(8), getdate(), 112), 1, 8)
       +'568'
       + replicate('0', 4-len(max_contractno)) --以指定的次数重复字符表达式
       + cast(max_contractno as varchar(4))
from (select
case
       when substring(max(my_contractno), 12, 4) is null then '1'
       else cast(cast(substring(max(my_contractno), 12, 4) as bigint) + 1 as varchar(8))
    end max_contractno
       from usercontractno
where datediff(day,adddate,getdate())=0
       ) s
create table [usercontractno](
    [id] [int] identity(1,1) not null,
    [applyuserdetailid] [int] not null,
    [my_contractno] [varchar](50) collate chinese_prc_ci_as not null constraint     [df_usercontractno_my_contractno] default (''),
    [adddate] [datetime] not null constraint [df_usercontractno_adddate] default (getdate()),
    [remarks] [varchar](50) collate chinese_prc_ci_as not null constraint [df_usercontractno_remarks] default (''),
constraint [pk_usercontractno] primary key clustered
([id] asc) on [primary]
) on [primary]
其它类似信息

推荐信息