溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

存儲函數(shù)實現(xiàn)字符串中存在數(shù)字的排序

發(fā)布時間:2020-06-19 19:18:17 來源:網(wǎng)絡(luò) 閱讀:249 作者:天簾雨幕 欄目:開發(fā)技術(shù)

問題:不做處理的排序:A2.1,A2.10,A2.2;希望的排序:A2.1,A2.2,A2.10

解決:

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

--11g解決排序字段存在數(shù)字和其他字符時的排序,對數(shù)字的部分統(tǒng)一左邊補6個零,再排序
 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ù)字的個數(shù)
       begin
           for num_count in 1..sumcount loop

                --把數(shù)字部分截取補零后替換字符串中的數(shù)字部分
               tempstr:=lpad(regexp_substr(newstr,'[0-9]+',1,num_count,'i'),6,'0');--左邊補零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中國2323-33.2齊位補零后:'|| toOder11g('1212中國2323-33.2') ||'');
 end;
--{

--10g解決排序字段存在數(shù)字和其他字符時的排序,對數(shù)字的部分統(tǒng)一左邊補6個零,再排序
--(與11G基本一致,只是需要用自定義的regexpcount(獲取數(shù)字的個數(shù),因為10G沒有這個函數(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ù)字個數(shù)
      begin
        for num_count in 1..sumcount loop
            tempstr:=lpad(regexp_substr(newstr,'[0-9]+',1,num_count,'i'),6,'0');--左邊補零6位
            newstr:=regexp_replace(newstr,'[0-9]+',tempstr,1,num_count,'i');
        end loop;
        return newstr;
      end;
    end toOder10g;
--{


--10g自定義返回正則表達式匹配的個數(shù)regexpcount(參數(shù):源字符串,正則表達式,字符串每次遇到數(shù)字部分截取掉,計數(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');--定義第二個匹配組開始的位置
   rightcount number:=0;
   tempstr varchar2(300):=sourcestr;
   begin
        while nexindex>0 loop
           rightcount:=rightcount+1;

            --從第二個匹配組開始的位置起,截取后長度=總長度-起始位置+1

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

            --重新為新的tempstr獲取第二個匹配組

           nexindex:=regexp_instr(tempstr,regexpstr,1,1,1,'i');
        end loop;
        return rightcount;
   end;
 end regexpcount;
 
 --使用
 set serveroutput on;   
 begin
   dbms_output.put_line('匹配該正則表達式的個數(shù)是:'|| regexpcount('1212AAA22ww','[0-9]+') ||'');
 end;
--}

***************************************Oracle實現(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表示第一個是數(shù)字,0表示沒有數(shù)字
     set @numindex=patindex('%[0-9]%',@sourcestr) --返回數(shù)字首次出現(xiàn)的位置
     set @ret=''   --不能定義為null
     set @strindex=0       
     while @numindex<>0       --當(dāng)不等于0時,即存在數(shù)字時進入循環(huán)
          begin 
               --從1開始截取,截取后長度=數(shù)字首次出現(xiàn)的位置-1,賦給ret,當(dāng)?shù)谝粋€字符就是數(shù)字時這個值

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

                        --if else 中間用begin end包裹,一句話時雖然不用也沒錯
                         set @ret=@ret+right(replicate('0',6)+@sourcestr,6)  

                         return ( @ret )
                    end
               else
                    begin

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


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

總結(jié):

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

                                                                                     



向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI