SQL Server:从string中选择整数到新表

时间:2017-11-07 23:37:52

标签: sql sql-server

我需要从不同格式的字符串(网址)中提取6-7位数的文档ID 我希望这可以作为数据库查询完成。

背景:

基本上我需要根据现有url中标识的docid构建一个新的url,这些是在EXTERNAL_REF数据库列中

例如:

http://*******/root/wrks/stores/default/default/pdf/docid/999762/dw_get

将成为

https://******/Prod/Anywhere/Web/Prod/ShowFunction/page?sk.DocumentId=999762&suite=ADT

示例源数据:

default/default/orig/docid/999980/pageno/1/dw_get
default/default/orig/docid/999986/pageno/1/dw_get
default/yyy/orig/docid/1000001/pageno/67/dw_get
78/default/orig/docid=1000480/pageno/1/dw_get
default/default/orig/docid=1000516/pageno/767/dw_get
default/default/orig/docid=1000527/pageno/1/dw_get
default/xxx/pdf/docid=999762/dw_get
default/default/orig/docid/1000581/pageno/1/dw_get
679/default/pdf/docid/999761/dw_get
default/default/orig/docid/1000590/pageno/1/dw_get
default/default/orig/docid/985747/dw_get

必填结果:

999980
999986
1000001
1000480
1000516
1000527
999762
1000581
999761
1000590
985747

4 个答案:

答案 0 :(得分:0)

字符串操作在SQL Server中很笨拙,但您可以这样做:

select v2.*
from t cross apply
     (values (stuff(url, 1, patindex('%[0-9][0-9][0-9][0-9][0-9][0-9]%', t.url) - 1, '') )
     ) v1(url1) cross apply
     (values (left(v1.url1, charindex('/', v1.url1) - 1))
     ) v2(url2);

Here是rextester。

答案 1 :(得分:0)

关键是使用char索引和子字符串来实现你想要的...... 这是一种方法来做到这一点..不一定是最好的方式......但仍然......我已经按步骤分解了......所以你可以看到发生了什么。

declare @string nvarchar (500) = 'default/default/orig/docid=999980/pageno/1/dw_get'
;with mycte as (
select 
@string as original_string
,   charindex('docid/',@string) [doc/]
,   charindex('docid=',@string) [doc=]
,case when 
        charindex('docid/',@string) <>0 then substring(@string,charindex('docid/',@string),100) 
            else  substring(@string,charindex('docid=',@string),100)  end as stripped
,replace(replace(case when 
        charindex('docid/',@string) <>0 then substring(@string,charindex('docid/',@string),100) 
            else  substring(@string,charindex('docid=',@string),100)  end,'docid/',''),'docid=','') remove_docid
)

Select 
        original_string
        ,[doc/]
        ,[doc=]
        ,stripped
        ,remove_docid
        ,left(remove_docid,CHARINDEX('/',remove_docid)-1) as document_no

 from mycte

答案 2 :(得分:0)

您可以使用以下

select substring(substring(string,charindex('docid',string,0)+6,len(string)),0,
                 charindex('/',substring(string,
                 charindex('docid',string,0)+6,len(string)),0))
from @temp

使用一个查询,您可以在与

相同的查询中组合这两个脚本
span.circle-name {
  -moz-border-radius:50%;
  -webkit-border-radius:50%;
  border-radius: 50%;
  border: 1px solid #5A9ADE;
  background-color: #FFFFFF;
  color: #5A9ADE;
  height: 60px;
  width: 60px;
  text-align: center;
  vertical-align: middle;
  padding-left: 14px;
  padding-right: 14px;
  padding-top: 15px;
  padding-bottom: 15px;
  font-size: 1.3em;
}

这是demo

希望这会对你有所帮助

答案 3 :(得分:0)

根据Monah的反应,我提出了:

select 
    Document_id, 
    EXTERNAL_REF, 
  CASE WHEN 
        substring(substring(EXTERNAL_REF,CHARINDEX('docid',EXTERNAL_REF,0)+6,len(EXTERNAL_REF)),0,CHARINDEX('/',substring(EXTERNAL_REF,CHARINDEX('docid',EXTERNAL_REF,0)+6,len(EXTERNAL_REF)),0)) = '' 
    THEN
    substring(substring(EXTERNAL_REF,CHARINDEX('docid',EXTERNAL_REF,0)+6,len(EXTERNAL_REF)),0,CHARINDEX('&',substring(EXTERNAL_REF,CHARINDEX('docid',EXTERNAL_REF,0)+6,len(EXTERNAL_REF)),0)) 
    ELSE 
    substring(substring(EXTERNAL_REF,CHARINDEX('docid',EXTERNAL_REF,0)+6,len(EXTERNAL_REF)),0,CHARINDEX('/',substring(EXTERNAL_REF,CHARINDEX('docid',EXTERNAL_REF,0)+6,len(EXTERNAL_REF)),0))
  END as DOCID 
    into [new table] 
    from [source Table]
    where EXTERNAL_REF like 'some String'

也许不是最优雅但对我有用。 谢谢大家及时的建议。