如何根据列中的值拆分列?

时间:2018-06-06 09:47:27

标签: sql-server

我有一个column1
“测试/优雅条件:TASK100:优雅项目\ | 2018.04.11 00:01:37 \ | N”

我需要将column1拆分为2列,输出应为

column1应该执行左边修剪直到:测试/优雅条件

column2应该从左边进行修剪:TASK100:优雅项目,在第一个\

之前

预期产出。

Column2                    |  Column3                            
------------------------     ----------------------------            
Test/ Elegant Conditions      TASK100: Elegant Item  

示例数据:

CREATE TABLE [Test]
(
    [Description] [varchar](250) NULL
)

insert into [Test] values ('Individual/ Team Actions\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Individual/ Team Actions\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Task/ Environmental Conditions : TW08: Equipment Integrity\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Task/ Environmental Conditions : TW01: Task Planning/ Preparation/ Manning\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Task/ Environmental Conditions : TW03: Work Procedures - Availability and Suitability\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Task/ Environmental Conditions\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Individual/ Team Actions\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Task/ Environmental Conditions : TW17: Ventilation\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Individual/ Team Actions\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Individual/ Team Actions : IT10: Hazard Recognition/ Perception\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Individual/ Team Actions : IT06: Procedural Compliance\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Task/ Environmental Conditions\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Task/ Environmental Conditions : HF01: Complacency/ Motivation/ Attitude\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Task/ Environmental Conditions\|2018.04.11 00:01:37\|N');

insert into [Test] values ('Organisational Factors : OS06: Design\|2018.04.11 00:01:37\|N');  


insert into [Test] values ('Individual/ Team Actions\|2018.04.11 00:01:37\|N');

2 个答案:

答案 0 :(得分:1)

您可以在此处使用基本字符串函数:

SELECT
    column1,
    SUBSTRING(column1, 1, CHARINDEX(':', column1) - 1) AS column2,
    SUBSTRING(column1,
              CHARINDEX(':', column1) + 1,
              CHARINDEX('\', column1) - CHARINDEX(':', column1) - 1) AS column3
FROM yourTable
WHERE column1 LIKE '%:%\%';

Demo

答案 1 :(得分:0)

这样的事情:

declare @a varchar(300)

select @a = 'Test/ Elegant Conditions : TASK100: Elegant Item\|2018.04.11 00:01:37\|N '

select  @a C1,
        ltrim(rtrim(left(@a, charindex(':', @a)-1))) C2,
        ltrim(rtrim(substring(@a, charindex(':', @a)+1, charindex('\', @a)-(charindex(':', @a)+1) ))) C3

输出

+---------------------------------------------------------------------------+--------------------------+-----------------------+
|                                    C1                                     |            C2            |          C3           |
+---------------------------------------------------------------------------+--------------------------+-----------------------+
| Test/ Elegant Conditions : TASK100: Elegant Item\|2018.04.11 00:01:37\|N  | Test/ Elegant Conditions | TASK100: Elegant Item |
+---------------------------------------------------------------------------+--------------------------+-----------------------+