如何仅使用select语句分割以逗号分隔的字符串?

时间:2018-08-21 22:45:49

标签: sql-server split

这里是情况:

  • 我无法实现自定义功能。请不要让我们辩论为什么。那就是座右铭的方式,我无法改变
  • 我需要使用某种SELECT语句以某种方式拆分逗号分隔的值
  • 我无法使用内置的STRING_SPLIT函数,因为由于权限问题,我需要将数据库兼容性设置为130,而我不能这样做

因此,在提到了所有这些内容之后,如何使用select语句吐出类似“ This,Is,A,Sentence”的内容?

我正在使用SQL Server 2008。

5 个答案:

答案 0 :(得分:2)

查询

let rid = null;
let shapesRy = [];

class Shape{
  constructor(path_a,path_b,morphingPath){
    this.target = getArgsRy(path_a);
    this.vals = getArgsRy(path_b);
    this.morphingPath = morphingPath;
    this.memory = [];
    for(let i=0; i < this.vals.length; i++){
    this.memory[i] = [];
    this.memory[i][0] = this.target[i].slice();
    this.memory[i][1] = this.vals[i].slice();     
    this.updatePath();
    }
  }

 updateValues() {
 for(let i = 0;i < this.memory.length; i++){ 
    let dist_x = this.target[i][1] - this.vals[i][1]; 
    let vel_x = dist_x/10;
    this.vals[i][1] += vel_x;

    let dist_y = this.target[i][2] - this.vals[i][2];
    let vel_y = dist_y/10;
    this.vals[i][2] += vel_y;
 }

    let dist_x = this.target[0][1] - this.vals[0][1]; 
    if (Math.abs(dist_x) < .01) {
    if(rid){window.cancelAnimationFrame(rid);
    rid = null;
    }

  }
}

 updatePath() {  
  let d=`M${this.vals[0][1]},${this.vals[0][2]}`;
  for(let i = 1;i < this.vals.length -1; i++){
  d += `L${this.vals[i][1]},${this.vals[i][2]}`
  }
  d +="Z"; 
  this.morphingPath.setAttributeNS(null, "d", d);
}
}


shapesRy.push(new Shape(t3Target,triangle3,triangle3));
shapesRy.push(new Shape(t4Target,triangle4,triangle4));



function Frame() {
  rid = window.requestAnimationFrame(Frame);
  shapesRy.map((s) => {
      s.updateValues();
      s.updatePath();
  })
}

svg.addEventListener(
  "mouseover",
  function() {
    if (rid) {
      window.cancelAnimationFrame(rid);
      rid = null;
    }   
    shapesRy.map((s) => {
     for(let i = 0;i < s.memory.length; i++){
       s.memory[i].reverse();
       s.target[i] = s.memory[i][1].slice();
     }
    })
    Frame();
  },
  false
);

svg.addEventListener(
  "mouseout",
  eAction,
  false
);

function eAction(){
  {
    if (rid) {
      window.cancelAnimationFrame(rid);
      rid = null;
    }

    shapesRy.map((s) => {
     for(let i = 0;i < s.memory.length; i++){
       s.memory[i].reverse();
       s.target[i] = s.memory[i][1].slice();
     }
    })
    Frame();
  }
}

function getArgsRy(path) {
    let d = path.getAttribute("d").replace(/\r?\n|\r/g, ""); //remove breaklines
    if (d.charAt(0) == "m") {
      d = "M" + d.slice(1);
    }
    let argsRX = /(?=[a-zA-Z])/;
    let args = d.split(argsRX);

    let ArgsRy = [];

    args.map(arg => {
      let argRy = arg
        .slice(1)
        .replace(/\-/g, " -")
        .split(/[ ,]+/);
      argRy.map((p, i) => {
        if (p == "") {
          argRy.splice(i, 1);
        }
      });

      for (let i = 0; i < argRy.length; i++) {
        argRy[i] = parseFloat(argRy[i]);
      }

      argRy.unshift(arg[0]);
      ArgsRy.push(argRy);
    });

    return ArgsRy;
  }

结果集:

 Declare @String nvarchar(500) = 'This,Is,A,Sentence';

SELECT  Split.a.value('.', 'VARCHAR(100)') Words
FROM (
      SELECT Cast ('<X>' 
                   + Replace(@String, ',', '</X><X>') 
                   + '</X>' AS XML) AS Data
     ) AS t CROSS APPLY Data.nodes ('/X') AS Split(a); 

答案 1 :(得分:0)

好吧,不是一个SELECT,但是您可以在不调用proc或UDF的情况下使用以下功能:

CREATE TABLE #OutputTable( SValues VARCHAR(100) )

DECLARE @StringInput VARCHAR(MAX)
        ,@StringTemp    VARCHAR(100)

WHILE LEN(@StringInput) > 0
BEGIN
    SET @StringTemp      = LEFT(@StringInput, 
                            ISNULL(NULLIF(CHARINDEX(',', @StringInput) - 1, -1),
                            LEN(@StringInput)))
    SET @StringInput = SUBSTRING(@StringInput,
                                    ISNULL(NULLIF(CHARINDEX(',', @StringInput), 0),
                                    LEN(@StringInput)) + 1, LEN(@StringInput))

    INSERT INTO #OutputTable ( SValues )
    VALUES ( @StringTemp )
END

#OutputTable将保留每个“单词”作为记录。然后,您可以根据需要查询它。

答案 2 :(得分:0)

如果可以使用CTE .....

╔══════════╗
║  Words   ║
╠══════════╣
║ This     ║
║ Is       ║
║ A        ║
║ Sentence ║
╚══════════╝

答案 3 :(得分:0)

值得怀疑的是:Find nth Occurrence in a string

DECLARE @Val varchar(200) = 'this,is,a,string'

;with t as (
    select @Val as val, 1 as starts, charindex(',', @Val) as pos 
    union all
    select @Val, pos + 1, charindex(',', @Val, pos + 1)
    from t
    where pos > 0
)
select 
    *, substring(@Val, starts, case when pos > 0 then pos - starts else len(@Val) end) token
from T
order by starts

答案 4 :(得分:0)

这就是我要做的:

    DECLARE @t TABLE     
(
ID INT,
Words VARCHAR(500)
)
INSERT @t VALUES (1,'This,Is,A,Sentence')



SELECT 
LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Words
FROM
(
SELECT CAST('<XMLRoot><RowData>' + REPLACE(Words,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM   @t
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)

结果:

|Words|
 This
 Is
 A
 Sentence