如何将String拆分为两列

时间:2013-10-11 12:57:33

标签: sql sql-server tsql

我有一张如下表

 Property   Value

 Adapter    [00000007] Intel(R) 82567LM Gigabit Network Connection
 Adapter    [00000009] VMware Virtual Ethernet Adapter for VMnet1
 Adapter    [00000012] Dell Wireless 1397 WLAN Mini-Card

从上面的表我想将列字符串拆分为两列,如下所示:

String1        String2
[00000007]     Intel(R) 82567LM Gigabit Network Connection
[00000009]     VMware Virtual Ethernet Adapter for VMnet1

请解决任何问题。

4 个答案:

答案 0 :(得分:0)

select 
  substring(Value,charindex('[',Value,1),charindex(']',value,charindex('[',Value,1)-1)) as String1
  ,ltrim(rtrim(substring(Value,charindex(']',value)+1,len(value)))) as String2
from Table1

<强> SQL Fiddle

答案 1 :(得分:0)

由于问题已经改变,这已经不再适用,但可能对其他人有用......

这似乎有效:

declare @value varchar(100) = '[00000007] Intel(R) 82567LM Gigabit Network Connection'

SELECT 
SUBSTRING(@value, 0, CHARINDEX(']', @value,0) + 1) firstPart,
LTRIM(RTRIM(SUBSTRING(@value, CHARINDEX(']', @value,0)+1, LEN(@value)))) secondPart

答案 2 :(得分:0)

如果您的案例很简单,那么:

select
    Property,
    substring(Value, 2, 8) as String1,
    -- or substring(Value, 1, 10) if you want [ and ]
    right(Value, len(Value) - 11) as String2
from Table1

<强> sql fiddle demo

答案 3 :(得分:0)

这是一个更通用的版本,可以在第一个空格( Fiddle demo )之前支持任意数量的字符:

SELECT LEFT(@S, CHARINDEX(' ',@S)) String1,
       RIGHT (@S, LEN(@S) - CHARINDEX(' ',@S)) String2

申请你的表:

SELECT LEFT(Value, CHARINDEX(' ',Value)) String1,
       RIGHT (Value, LEN(Value) - CHARINDEX(' ',Value)) String2
FROM yourTable