用sql字符串中的特殊字符替换数字 [英] Replace numbers with special characters in sql string

查看:326
本文介绍了用sql字符串中的特殊字符替换数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sql中有一个包含字母数字值的字符串(例如:hello 123 world 456)。

我想用一些特殊字符替换数字值,即123465,如@#%&* ^分别。我尝试编写许多功能,但我没有得到我想要的功能。



建议将不胜感激。



谢谢。

解决方案

如果您使用的是sql server,请尝试下面给出的代码。您可以使用以下代码创建函数,并在查询中使用它。尝试&如果找到相同的范围,请改进代码 -



 声明  @ str   varchar  100 )= '  mad123hu' 
声明 @ i int = 1

while @ i< = len( @ str
开始
声明 @ val varchar 1
set @ val = substring( @ str @i 1
if ascii( @ val )> = 48 ascii( @ val )< = 57
开始
声明 @ newchar varchar 1

set @ newchar = case ascii( @ val
何时 48 然后 ' !'
49 然后 ' @'
when 50 ' #'
51 然后 '

<当 52 then ' %'
when 53 ' ^'
54 然后 ' *'
when 55 然后 ' < span class =code-string>('
56 < span class =code-keyword>然后 ' )'
< span class =code-keyword>当 57 然后 ' _'
end
set @ str = replace( @ str @ val @ newc har
end
set @ i + = 1
end
选择 @ str


你可以试试这个...

 声明  @ s   varchar  100 ), @ result   varchar  100 
set @s = ' hello 123 world 456'
set @ result = ' '

选择 @ result = @ result + 案例 号码喜欢 ' [0-9]' 然后
案例 号码 in ' 0'' 5'然后 ' @'
其他 案例 编号 ' 1'' 6'然后 ' #'
其他 案例 编号 ' 2'' 7'然后 ' %'
其他 案例 编号 ' 3'' 8'然后 ' &'
其他 ' *'
结束
结束
结束
结束
其他号码
结束
From select substring( @ s ,number, 1 as number
来自选择号码来自 master..spt_values
其中​​ type = < span class =code-string>' p' number 1 len( @ s )) as t
as t

选择 @ result ;



输出:

你好#%&世界* @#


I've a string in sql which contains alphanumeric value (eg: hello 123 world 456).
I want to replace numeric values i.e., 123465 with some special characters like @#%&*^ respectively. I tried to script many function but I'm not getting exactly what I want.

Suggestions will be appreciated.

Thanks.

解决方案

if you are using sql server then try the given below code. You can create a function using the below code and use it in your queries. Try & improve the code if you find any scope for the same -

declare @str varchar(100)='mad123hu'
declare @i int=1

while @i<=len(@str)
    begin
        declare @val varchar(1)
        set @val=substring(@str,@i,1)
        if ascii(@val) >=48 and ascii(@val)<=57
            begin
                declare @newchar varchar(1)

                set @newchar=case ascii(@val)
                                when 48 then '!'
                                when 49 then '@'
                                when 50 then '#'
                                when 51 then '


' when 52 then '%' when 53 then '^' when 54 then '*' when 55 then '(' when 56 then ')' when 57 then '_' end set @str=replace(@str,@val,@newchar) end set @i+=1 end select @str


u can try this...

Declare @s varchar(100),@result varchar(100)
set @s='hello 123 world 456'
set @result=''

Select @result = @result + Case When number like '[0-9]' Then
                                Case When number in('0','5') Then '@'
                                      Else Case When number in('1','6') Then '#'
                                           Else Case When number in('2','7') Then '%'
                                                Else Case When number in('3','8') Then '&'
                                                     Else '*'
                                                End
                                           End
                                      End
                                 End
                                 Else number
                           End
From (select substring(@s,number,1) as number
      from (select number from master..spt_values
      where type='p' and number between 1 and len(@s)) as t
     ) as t

select @result;


Output:

hello #%& world *@#


这篇关于用sql字符串中的特殊字符替换数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆