SQL将数据拉到另一列SUBSTRING CHARINDEX

时间:2018-10-24 15:01:27

标签: sql sql-server

下面的数据示例位于数据库的一个字段中

Sizing Specification: Type A Specification, Equipment: For Vehicles with 
type x, Connectors type: Push in, Weight [kg]: 0.65, Voltage [V]: 6, Number
of Poles: 2, Number of Inlets / Outlets: 1

如果重量存在,我想尝试将重量提取到单独的列中。到目前为止,我的select语句中包含以下代码。

SUBSTRING(dbo.fullresult.PRODINFO, CHARINDEX('Weight ', dbo.fullresult.PRODINFO), 17) 

这使我Weight [kg]: 0.65进入单独的列,但我只想显示0.65。理想情况下,我需要在权重[kg]之后和逗号之前的文本,以避免仅在存在权重的地方出现多余的字符。

4 个答案:

答案 0 :(得分:1)

这是一种方法,只需将@string替换为您的列名:

DECLARE @STRING VARCHAR(200) = 'Sizing Specification: Type A Specification, Equipment: For Vehicles with type x, Connectors type: Push in, Weight [kg]: 0.65, Voltage [V]: 6, Number of Poles: 2, Number of Inlets / Outlets: 1'

SELECT SUBSTRING(SUBSTRING(@STRING, CHARINDEX('Weight ', @STRING), 25), CHARINDEX(':',SUBSTRING(@STRING, CHARINDEX('Weight ', @STRING), 25))+2, CHARINDEX(',',SUBSTRING(@STRING + ',', CHARINDEX('Weight ', @STRING), 25))-2 -CHARINDEX(':',SUBSTRING(@STRING, CHARINDEX('Weight ', @STRING), 25)))

如果分解,可能更容易阅读。它正在:之后抽出,Weight之间的块:

DECLARE @STRING VARCHAR(200) = 'Sizing Specification: Type A Specification, Equipment: For Vehicles with type x, Connectors type: Push in, Weight [kg]: 0.65, Voltage [V]: 6, Number of Poles: 2, Number of Inlets / Outlets: 1'

SELECT SUBSTRING(@STRING, CHARINDEX('Weight ', @STRING), 25)

DECLARE @STRING2 VARCHAR(200) = SUBSTRING(@STRING, CHARINDEX('Weight ', @STRING), 25)

SELECT SUBSTRING(@STRING2, CHARINDEX(':',@STRING2)+2, CHARINDEX(',',@STRING2)-2 -CHARINDEX(':',@STRING2))

答案 1 :(得分:1)

另一个适合您的选择

DECLARE @TestData TABLE
    (
        [StringData] NVARCHAR(MAX)
    );

INSERT INTO @TestData (
                          [StringData]
                      )
VALUES ( 'Sizing Specification: Type A Specification, Equipment: For Vehicles with type x, Connectors type: Push in, Voltage [V]: 6, Number of Poles: 2, Number of Inlets / Outlets: 1, Weight [kg]: 50000.00' )
,( 'Sizing Specification: Type A Specification, Equipment: For Vehicles with type x, Connectors type: Push in, Voltage [V]: 6,Weight [kg]: 0.65, Number of Poles: 2, Number of Inlets / Outlets: 1' );



SELECT SUBSTRING(
                    [StringData]
                  , CHARINDEX(
                                ':'
                              , [StringData]
                              , CHARINDEX('Weight', [StringData])
                            ) + 1
                  , CASE WHEN CHARINDEX(
                                           ','
                                         , [StringData]
                                         , CHARINDEX('Weight', [StringData])
                                       ) = 0 THEN LEN([StringData]) + 1  --Case statement check to see if comma is after weight, if it isn't then Weight is at the end.
                         ELSE
                             CHARINDEX(
                                          ','
                                        , [StringData]
                                        , CHARINDEX('Weight', [StringData])
                                      )
                    END
                    - CHARINDEX(
                                   ':'
                                 , [StringData]
                                 , CHARINDEX('Weight', [StringData])
                               ) - 1
                )
FROM   @TestData
WHERE  CHARINDEX('Weight', [StringData]) <> 0;

答案 2 :(得分:0)

您可以尝试以下代码:

SELECT LEFT(SUBSTRING(dbo.fullresult.PRODINFO, PATINDEX('%[0-9.]%'
,dbo.fullresult.PRODINFO),8000),
PATINDEX('%[^0-9.]%',SUBSTRING(dbo.fullresult.PRODINFO,
PATINDEX('%[0-9.]%',dbo.fullresult.PRODINFO),8000) + 'X')- 1)

答案 3 :(得分:0)

将字符串放入变量...

DECLARE @document varchar(500);  
SELECT @document = 'Sizing Specification: Type A Specification, Equipment: For Vehicles with type x, Connectors type: Push in, Weight [kg]: 0.65, Voltage [V]: 6, Number of Poles: 2, Number of Inlets / Outlets: 1';  
SELECT SUBSTRING(@document,(CHARINDEX('[kg]', @document)+6),4)  as WeightKG

从KG点开始的子字符串(&6个空格到实际重量)