从freetext中提取可变长度的字符串

时间:2015-03-02 17:17:00

标签: sql sql-server

请参阅下面的DDL:

create table #Test (comments varchar(1000))

 insert into #Test values ('Nominal linking: NOMINAL_ID 9 has been established to be the same person as NOMINAL_ID 1 and the nominals have been linked.')
insert into #Test values ('Nominal linking: NOMINAL_ID 12 has been established to be the same person as NOMINAL_ID 641 and the nominals have been linked.')
insert into #Test values ('Nominal linking: NOMINAL_ID 123 has been established to be the same person as NOMINAL_ID 2019 and the nominals have been linked.')
insert into #Test values ('Nominal linking: NOMINAL_ID 1234 has been established to be the same person as NOMINAL_ID 22222 and the nominals have been linked.')
insert into #Test values ('Nominal linking: NOMINAL_ID 12345 has been established to be the same person as NOMINAL_ID 22 and the nominals have been linked.')

我试图在SQL查询中返回以下内容,并且不相信它可以完成:

MasterID  ChildID   
9         1
12        641
123       2019
1234      22222
12345     22

我意识到这张桌子的设计很差。这是一个外部开发的系统。

3 个答案:

答案 0 :(得分:2)

提取数字的功能

CREATE FUNCTION get_Numbers
( @String NVARCHAR(1000)
)
RETURNS VARCHAR(50)
AS 
BEGIN
 declare @RtnVal nvarchar(50) =''; 

    select @RtnVal = @RtnVal + x.thenum 
    from 
    ( 
        select substring(@String, number, 1) as thenum, number 
        from master..spt_values 
        where substring(@String, number, 1) like '[0-9]' and type='P'
    ) x 
    order by x.number 

   RETURN @RtnVal;
END

<强>查询

SELECT dbo.get_Numbers(LEFT(comments , LEN(comments)/2))  AS Master_ID
     , dbo.get_Numbers(RIGHT(comments , LEN(comments)/2)) AS Child_ID
FROM #Test

<强>结果

╔═══════════╦══════════╗
║ Master_ID ║ Child_ID ║
╠═══════════╬══════════╣
║         9 ║        1 ║
║        12 ║      641 ║
║       123 ║     2019 ║
║      1234 ║    22222 ║
║     12345 ║       22 ║
╚═══════════╩══════════╝

答案 1 :(得分:0)

您可以将此作为表值函数的基础。此版本不依赖于一个字符串位于字符串的一半而另一个值位于字符串的另一半中。

DECLARE @String NVARCHAR(1000)='No 9333 ha_ID 112242'
DECLARE @FirstNumber BIGINT=NULL
DECLARE @SecondNumber BIGINT=NULL

DECLARE @Counter SMALLINT=0
DECLARE @CurrentNum VARCHAR(20);

SET @CurrentNum=''
WHILE @Counter<=LEN(@String)+1
BEGIN
    print SUBSTRING(@String,@Counter,1)
    IF(@Counter=LEN(@String)+1 AND LEN(@CurrentNum)>0)
        BEGIN
            IF @FirstNumber IS NULL SET @FirstNumber=CONVERT(BIGINT,@CurrentNum)
            IF @FirstNumber IS NOT NULL SET @SecondNumber=CONVERT(BIGINT,@CurrentNum)
            SET @CurrentNum=''
        END
    ELSE IF SUBSTRING(@String,@Counter,1) like '[0-9]'
        BEGIN
            SET @CurrentNum=@CurrentNum+SUBSTRING(@String,@Counter,1)
        END
    ELSE IF LEN(@CurrentNum)>0
        BEGIN
            IF @FirstNumber IS NULL SET @FirstNumber=CONVERT(BIGINT,@CurrentNum)
            IF @FirstNumber IS NOT NULL SET @SecondNumber=CONVERT(BIGINT,@CurrentNum)
            SET @CurrentNum=''
        END

    SET @Counter=@Counter+1
END

select @FirstNumber [First Number],@SecondNumber [Second Number]

答案 2 :(得分:0)

使用charindex和其他字符串函数的暴力:

选择SUBSTRING(评论,29,CHARINDEX(&#39;已经&#39;,评论) - 29) 作为master_id, left(substring(SUBSTRING(评论,charindex(&#39; as nominal_id&#39;,评论)+ 14,CHARINDEX(&#39;以及名义已被链接。&#39;,评论)),1, 6),CHARINDEX(&#39;&#39;,substring(SUBSTRING(评论,charindex(&#39; as nominal_id&#39;,评论)+ 14,CHARINDEX(&#39;和名义已被链接。&#39;,评论)),1,6)))作为#test

的child_id