不知道位置或长度时使用SubString

时间:2012-07-12 07:12:12

标签: sql

任何人都可以帮助我,因为我需要在"Reference=" in the enclosed?之后提取数字。不可能知道数字之前或之后的字符数以及数字的长度(也可以是字母和数字)但它永远是

After "Reference=" Before ",Description"

Object=CTSENORaanG,Reference=0000021357,Description=Test,Currency=EUR,Initial_Date=15Aug2011....

字符串来自具有大量行的表,我需要将每行的引用插入到另一个表中

3 个答案:

答案 0 :(得分:2)

SQL Server

declare @str varchar(1000)
set @str='Object=CTSENORaanG,Reference=0000021357,Description=Test,Currency=EUR,Initial_Date=15Aug2011....';

select substring(col,1,charindex(',',col)-1) from
(
select substring(@str,charindex('Reference=',@str)+10,100) as col
) as t;

的MySQL

set @str:='Object=CTSENORaanG,Reference=0000021357,Description=Test,Currency=EUR,Initial_Date=15Aug2011....';

select substring(col,1,locate(',',col)-1) from
(
select substring(@str,locate('Reference=',@str)+10,100) as col
) as t;

答案 1 :(得分:1)

如果分隔符始终是逗号,,则可以拆分字符串。然后,如果分隔的子字符串也与=一致,则可以进一步拆分它。一旦你捞出Reference,你就会得到它的等价值。

/* your test string */
string s = "Object=CTSENORaanG,Reference=0000021357,Description=Test,Currency=EUR,Initial_Date=15Aug2011";     
string[] arr = s.Split(',');

/* will contain the value you're looking for */    
string target = String.Empty;

foreach (string item in arr)
{
   string[] entry = item.Split('=');
   if (entry.Length == 2 && entry[0] == "Reference")
   {
      target = entry[1];
      break;
   }
}

答案 2 :(得分:0)

use
pos = explode(",",string);
foreach(pos as po)
{
    p = explode("=",po)
    i=0;
    foreach(p as a)
    {
        if(a[i]== Reference)
        {
            echo p[i+1];
            i++;
        }
    }
}