查找“{”和“}”中的文本并将其插入表中

时间:2015-09-10 12:23:05

标签: sql sql-server

请帮我写一下存储过程。在这里,我有一个像下面给出的输入。

{local.txt_concept_id} != "" & {local.txt_ICD_code} != "" & 
{local.txt_diagnosis_uniq_id} != "" & {local.txt_ICD_desc} != ""&
{FIND( {local.txt_ICD_code} , "asp", 0 )}!=-1&
{LEN( {local.txt_diagnosis_uniq_id})}

我想从中获取包含在开括号和近大括号内的文本。这必须在SQL Server本身中完成。所以没有高级语言 (C#,Java,PHP)。

请帮我查询 预期结果表将具有类似

的值
local.txt_concept_id
local.txt_ICD_code
local.txt_ICD_desc
local.txt_diagnosis_uniq_id

1 个答案:

答案 0 :(得分:1)

DECLARE @myString NVARCHAR(max) = 
'{local.txt_concept_id} != "" FDKJFKJ TRJEHTJH TREKJTJT & {local.txt_ICD_code} != "" &  {local.txt_diagnosis_uniq_id} != "" & {local.txt_ICD_desc} != ""& {FIND( {local.txt_ICD_code} , "asp", 0 )}!=-1& {LEN( {local.txt_diagnosis_uniq_id_IN_LENGTH})}'
; 
DECLARE @i     INT, 
        @start INT, 
        @end   INT, 
        @new NVARCHAR(max)
DECLARE @FieldTable TABLE 
  ( 
     field_name VARCHAR(200) 
  ) 

WHILE ( Len(@myString) > 0 ) 
  BEGIN 
      SET @new = '' 

      SELECT @start = Patindex('%{%', @mystring), 
             @end = Patindex('%}%', @myString) 

      IF( @end < @start ) 
        BEGIN 
            SELECT @myString = RIGHT(@myString, Len(@mystring) - 1) 
        END 
      ELSE IF ( Patindex('%}%', @myString) = 0 
            OR Patindex('%{%', @myString) = 0 ) 
        BEGIN 
            SET @myString = '' 
        END 
      ELSE 
        BEGIN 
            SELECT @new = Substring(@mystring, @start, @end - @start + 1) 
            SELECT @myString = Replace(@mystring, @new, '') 
            SET @new = Reverse(Replace(@new, '}', '')) 
            SET @i = Patindex('%{%', @new) - 1  
            SET @new = Reverse(LEFT(@new, @i)) 
        END 

      IF( Len(@new) > 1 
          AND Patindex('%)%', @new) = 0 
          AND Patindex('%(%', @new) = 0 ) 
        INSERT INTO @FieldTable 
        VALUES      (@new) 
  END 

SELECT * 
FROM   @FieldTable