2015-03-02 26 views
0

可變長度的拉拔字符串請參閱下面的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 

我意識到這張表的設計很差。它是一個外部開發的系統。

+0

你應該可以用PatIndex和SubString來完成它。 – 2015-03-02 17:20:00

回答

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 ║ 
╚═══════════╩══════════╝ 
+0

+1需要注意的是,如果一個數字非常小,另一個數字包含許多數字,則選擇的邏輯將不正確。 – UnhandledExcepSean 2015-03-02 17:41:06

+0

@Ghost,你能舉個例子嗎? – w0051977 2015-03-02 17:48:14

+0

名義鏈接:NOMINAL_ID 1已被建立爲與NOMINAL_ID 222222222222222222222222222222222222222220000000000000000000000000222222222222相同的人,並且名稱已鏈接。 – UnhandledExcepSean 2015-03-02 17:51:34

0

你能我們e作爲表值函數的基礎。該版本不依賴於一個值位於字符串的一半,另一個位於字符串的另一半。

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 @[email protected]+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 @[email protected]+1 
END 

select @FirstNumber [First Number],@SecondNumber [Second Number] 
0
使用CHARINDEX等字符串函數

蠻力:

選擇SUBSTRING(註釋29,CHARINDEX( '一直',評論) - 29) 作爲master_id, 左(串(SUBSTRING(評論,charindex('as nominal_id',評論)+ 14,CHARINDEX('和nominals已被鏈接。',評論)),1,6),CHARINDEX('',substring(SUBSTRING(comments,charindex('as ),14),CHARINDEX('和註釋已經被鏈接。',評論)),1,6)))作爲#test的child_id

+0

我試過了。在28000行數據上ali更加優雅,解決方案花費了2:06(兩分六秒),我的解決方案耗時1秒。 – 2015-03-02 18:47:44

+0

謝謝。我真的在尋找一個sql語句。我會明天測試它並回復你。 – w0051977 2015-03-02 23:38:08