将大括号之间的accounid和dbid分成两列

时间:2016-12-15 04:57:49

标签: sql sql-server tsql

我有一个查询来分别在大括号之间拆分accounid和dbids。 例如:Course.findOne({ _id: req.params.id , 'sections.status': 'active' }) .exec((err, course) => { if (err) { logger.error(err); res.status(500).json({ success: false, message: 'Error in getting data' }); } else if (!course) { res.status(404).json({ success: false, message: 'No course details found' }); } else { res.json({ success: true, course }); } });

[14801].[42]

但我在逗号之后无法分开acid scid 14801 42 例如:

,

需要查询将此数据拆分为行

3 个答案:

答案 0 :(得分:0)

您需要创建将值拆分为行的函数

create  function [dbo].[udf_splitstring] (@tokens    varchar(max),
                                         @delimiter varchar(5))
returns @split table (
  token varchar(200) not null )
as
  begin
      declare @list xml

      select @list = cast('<a>'
                          + replace(@tokens, @delimiter, '</a><a>')
                          + '</a>' as xml)

      insert into @split
                  (token)
      select ltrim(t.value('.', 'varchar(200)')) as data
      from   @list.nodes('/a') as x(t)

      return
  end

 select * from  into #a udf_splitstring ('[27784].[41],[27781].[41],[27779].[41]',',')

输出

[27784].[41]
[27781].[41]
[27779].[41]

将结果存储在一个临时表中

 SELECT TOKEN,REPLACE(REPLACE(SUBSTRING(TOKEN,0,CHARINDEX('.',TOKEN)),'[',''),']','') AS first_id
        ,REPLACE(REPLACE(REVERSE(SUBSTRING(REVERSE(TOKEN),0,CHARINDEX('.',REVERSE(TOKEN)))),'[',''),']','') AS second_id
FROM #a

输出

TOKEN   first_id    second_id
[27784].[41]    27784   41
[27781].[41]    27781   41
[27779].[41]    27779   41

答案 1 :(得分:0)

试试这个:

DECLARE @START_ID VARCHAR(100)='[27784].[41],[27781].[41],[27779].[41]'
DECLARE @ID VARCHAR(MAX)
DECLARE @COUNT INT
DECLARE @TEMP TABLE(C1 VARCHAR(MAX))

    WHILE( CHARINDEX(',',@START_ID))>0
    BEGIN
    INSERT INTO @TEMP SELECT SUBSTRING(@START_ID,0,CHARINDEX(',',@START_ID))
    SET @START_ID=(SELECT REPLACE(@START_ID,(SUBSTRING(@START_ID,0,CHARINDEX(',',@START_ID)+1)),''))
    END
    INSERT INTO @TEMP SELECT @START_ID

SELECT C1,REPLACE(REPLACE(SUBSTRING(C1,0,CHARINDEX('.',C1)),'[',''),']','') AS SOURCE_ACCOUT_ID
        ,REPLACE(REPLACE(REVERSE(SUBSTRING(REVERSE(C1),0,CHARINDEX('.',REVERSE(C1)))),'[',''),']','') AS SOURCE_DATABASE_ID
FROM @TEMP

答案 2 :(得分:0)

select      n.ids.value ('id[1]','int') as accountid
           ,n.ids.value ('id[2]','int') as dbid

from       (select      cast (replace('<r><e><id>'+replace(replace(replace(
                            ids,'[',''),']',''),',','</id></e><e><id>')+
                                 '</id></e></r>','.','</id><id>')  as xml) as x
            from        mytable
            ) t
            cross apply x.nodes ('/r/e') n(ids)
+-----------+------+
| accountid | dbid |
+-----------+------+
| 27784     | 41   |
+-----------+------+
| 27781     | 41   |
+-----------+------+
| 27779     | 41   |
+-----------+------+
| 28021     | 30   |
+-----------+------+
| 28024     | 30   |
+-----------+------+
| 29007     | 56   |
+-----------+------+

用于演示的DDL + DML

create table mytable (ids varchar(1000))

insert into mytable values 

  ('[27784].[41],[27781].[41],[27779].[41]')
, ('[28021].[30],[28024].[30]')
, ('[29007].[56]')