存儲(chǔ)函數(shù)實(shí)現(xiàn)字符串中存在數(shù)字的排序-創(chuàng)新互聯(lián)

問(wèn)題:不做處理的排序:A2.1,A2.10,A2.2;希望的排序:A2.1,A2.2,A2.10

成都創(chuàng)新互聯(lián)專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)、龍子湖網(wǎng)絡(luò)推廣、微信小程序、龍子湖網(wǎng)絡(luò)營(yíng)銷(xiāo)、龍子湖企業(yè)策劃、龍子湖品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪(fǎng)、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供龍子湖建站搭建服務(wù),24小時(shí)服務(wù)熱線(xiàn):18980820575,官方網(wǎng)址:muchs.cn

解決:

******************************************Oracle實(shí)現(xiàn)方式*******************************************

--11g解決排序字段存在數(shù)字和其他字符時(shí)的排序,對(duì)數(shù)字的部分統(tǒng)一左邊補(bǔ)6個(gè)零,再排序{
 create or replace function toOder11g(str varchar2) return varchar2 as
 begin
      declare num_count number:=1;
      query_sql varchar2(300);
      newstr varchar2(300):=str;--返回的新字符串
      tempstr varchar2(300);--表示替換成xx字符
      sumcount number:=regexp_count(str,'[0-9]+',1,'i');--查找字符串中匹配數(shù)字的個(gè)數(shù)
       begin
          for num_count in 1..sumcount loop

                --把數(shù)字部分截取補(bǔ)零后替換字符串中的數(shù)字部分
               tempstr:=lpad(regexp_substr(newstr,'[0-9]+',1,num_count,'i'),6,'0');--左邊補(bǔ)零6位

               newstr:=regexp_replace(newstr,'[0-9]+',tempstr,1,num_count,'i');
          end loop;
           return newstr;
        end;
 end toOder11g;
 --使用
 set serveroutput on;
 begin
  dbms_output.put_line('1212中國(guó)2323-33.2齊位補(bǔ)零后:'|| toOder11g('1212中國(guó)2323-33.2') ||'');
 end;
--{

--10g解決排序字段存在數(shù)字和其他字符時(shí)的排序,對(duì)數(shù)字的部分統(tǒng)一左邊補(bǔ)6個(gè)零,再排序
--(與11G基本一致,只是需要用自定義的regexpcount(獲取數(shù)字的個(gè)數(shù),因?yàn)?0G沒(méi)有這個(gè)函數(shù))){
 create or replace function toOder10g(str varchar2) return varchar2 as
   begin
     declare num_count number:=1;
     query_sql varchar2(300);
     newstr varchar2(300):=str;--返回的新字符串
     tempstr varchar2(300);--表示替換成xx字符
     sumcount number:=regexpcount(str,'[0-9]+');--數(shù)字個(gè)數(shù)
     begin
       for num_count in 1..sumcount loop
           tempstr:=lpad(regexp_substr(newstr,'[0-9]+',1,num_count,'i'),6,'0');--左邊補(bǔ)零6位
           newstr:=regexp_replace(newstr,'[0-9]+',tempstr,1,num_count,'i');
       end loop;
       return newstr;
     end;
   end toOder10g;
--{

--10g自定義返回正則表達(dá)式匹配的個(gè)數(shù)regexpcount(參數(shù):源字符串,正則表達(dá)式,字符串每次遇到數(shù)字部分截取掉,計(jì)數(shù)加一,直到不存在數(shù)字){
   --定義
 create or replace function regexpcount(sourcestr varchar2,regexpstr varchar2) return number as
 begin
  declare nexindex number:=regexp_instr(sourcestr,regexpstr,1,1,1,'i');--定義第二個(gè)匹配組開(kāi)始的位置
  rightcount number:=0;
  tempstr varchar2(300):=sourcestr;
  begin
       while nexindex>0 loop
          rightcount:=rightcount+1;

            --從第二個(gè)匹配組開(kāi)始的位置起,截取后長(zhǎng)度=總長(zhǎng)度-起始位置+1

           tempstr:=substr(tempstr,nexindex,length(tempstr)-nexindex+1);--
          if(tempstr is null) then
                 return rightcount;
          end if;

            --重新為新的tempstr獲取第二個(gè)匹配組

           nexindex:=regexp_instr(tempstr,regexpstr,1,1,1,'i');
       end loop;
       return rightcount;
  end;
 end regexpcount;

 --使用
 set serveroutput on;
 begin
  dbms_output.put_line('匹配該正則表達(dá)式的個(gè)數(shù)是:'|| regexpcount('1212AAA22ww','[0-9]+') ||'');
 end;
--}

***************************************Oracle實(shí)現(xiàn)方式end************************************

***************************************sqlserver***********************************************

CREATE FUNCTION [titans_schema].[toorder](@sourcestr [varchar](50))
RETURNS [varchar](50) AS
begin
    declare @ret [varchar](50),@numindex int, @strindex int  --numindex不等于0,1表示第一個(gè)是數(shù)字,0表示沒(méi)有數(shù)字
    set @numindex=patindex('%[0-9]%',@sourcestr) --返回?cái)?shù)字首次出現(xiàn)的位置
     set @ret=''  --不能定義為null
    set @strindex=0
     while @numindex<>0       --當(dāng)不等于0時(shí),即存在數(shù)字時(shí)進(jìn)入循環(huán)
          begin
               --從1開(kāi)始截取,截取后長(zhǎng)度=數(shù)字首次出現(xiàn)的位置-1,賦給ret,當(dāng)?shù)谝粋€(gè)字符就是數(shù)字時(shí)這個(gè)值

                --是“”
               set @ret=@ret+substring(@sourcestr,1,(@numindex-1)) --aa
               --源字符串去掉非數(shù)字部分(從右邊開(kāi)始截取,長(zhǎng)度=總長(zhǎng)度-numindex+1)
               set @sourcestr=right(@sourcestr,len(@sourcestr)-@numindex+1)
               --截取數(shù)字部分補(bǔ)零成6位后跟前面的非數(shù)字部分合并
               --先獲取非數(shù)字部分的index,如果為0,說(shuō)明已經(jīng)是末尾,補(bǔ)零后結(jié)束循環(huán)
               set @strindex=patindex('%[^0-9]%',@sourcestr)
               if @strindex=0
                    begin

                        --if else 中間用begin end包裹,一句話(huà)時(shí)雖然不用也沒(méi)錯(cuò)
                         set @ret=@ret+right(replicate('0',6)+@sourcestr,6)

                         return ( @ret )
                    end
               else
                    begin

                        --從1開(kāi)始截取長(zhǎng)度=非數(shù)字首次出現(xiàn)的位置-1,截取到的數(shù)字再進(jìn)行6個(gè)0的補(bǔ)零齊位
                         set @ret=@ret+right(replicate('0',6)+substring(@sourcestr,1,(@strindex-1)),6)
                    end
               --源字符串再去掉數(shù)字部分,接下來(lái)就進(jìn)入了循環(huán)(非數(shù)字,數(shù)字,非數(shù)字....)
               set @sourcestr=right(@sourcestr,len(@sourcestr)-patindex('%[^0-9]%',@sourcestr)+1)
               --改變循環(huán)標(biāo)識(shí)變量的值
               set @numindex=patindex('%[0-9]%',@sourcestr)
      end
   return ( @ret )
end

***************************************sqlserver實(shí)現(xiàn)方式end************************************

總結(jié):

    以上函數(shù)均經(jīng)過(guò)本人測(cè)試,如果有錯(cuò)誤,改進(jìn),疑問(wèn)的地方,請(qǐng)留言。文中使用的是函數(shù)在百度中均可查到,所以不再重復(fù)。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)頁(yè)題目:存儲(chǔ)函數(shù)實(shí)現(xiàn)字符串中存在數(shù)字的排序-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://muchs.cn/article24/degjce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、網(wǎng)站內(nèi)鏈網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站改版、網(wǎng)站制作品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)