TSQL从列中提取代码和描述

时间:2014-11-12 10:19:38

标签: tsql

我需要从列中提取代码和描述,如下所示:

USERTEXT1,Investment type Investor Type   ,AD~Advisory FOF~Fund of Funds SP~Structured Product FE~Feeder EX~Exclude CDV~CD Vehicle USERTEXT2,Investment type Advisory Capacity   ,ND~Non Discretionary DI~Discretionary USERTEXT3,Investment type CD Input   ,WI~with involvement NI~no involvement USERTEXT4,Investment type Investment Type CD   ,PRIM~Primary SEC~Secondary GP~GP USERTEXT5, Current or Realised Investment   ,C~Current R~Realised USERTEXT6,Identity UNIQUE ID   , USERTEXT7,Client specific PICA Acc. Method   ,COST~Cost EQ~Equity USERBOOL1,Investment type Default Investor?   , USERDATE1,Investment type Default Date   , USERTEXT8,Client specific Naming Convention   , USERTEXT9,Client specific Property Number   ,

我期望的输出是:

Field Code Description
USERTEXT1 AD Advisory
USERTEXT1 FOF Fund of Funds
USERTEXT1 SP Structured Product
(...)
USERTEXT2 ND Non Discretionary
USERTEXT2 DI Discretionary
(...)
USERTEXT6 NULL NULL

我只对USERTEXTxx字段感兴趣,因为您可以看到并非所有字段都有值。 我正在与功能和不同的循环战斗,但无法获得正确的输出:/ 我非常感谢你的帮助。

以下是我迄今为止创建的功能,但也许还有其他更快更好的解决方案。我的功能在100%不起作用

Create function dbo.rad_AdditionalField(@table varchar(50), @field_name varchar(50), @code varchar(50))
RETURNS varchar(100)
AS
BEGIN

-- select dbo.rad_additionalfield('vcsubscriber', 'usertext1','FOF')
--Declare @table varchar(50) = 'VCSUBSCRIBER'
Declare @add_field varchar(max) 
--Declare @field_name varchar(50) = 'USERTEXT1'
--Declare @code varchar(10) = 'FOF'

Declare @individual varchar(4000) = NULL
Declare @int int = 0
Declare @field varchar(100)
Declare @descr varchar(200)
Declare @value varchar(max)

Declare @tab table(field varchar(100), descr varchar(200), value varchar(max))

select @add_field = USERTYPES1 
from ADMTABLE
where CLASS1=@table


WHILE LEN(@add_field) > 0
BEGIN
    IF PATINDEX('%USER%',@add_field) > 0
    BEGIN
        set @int=@int+1
        SET @individual = SUBSTRING(@add_field, 0, PATINDEX('%USER%',@add_field))
        if @int > 1 
        BEGIN
            SELECT @field=Data from dbo.rad_split('U' + @individual,',') where ID = 1
            SELECT @descr=Data from dbo.rad_split('U' + @individual,',') where ID = 2
            SELECT @value=Data from dbo.rad_split('U' + @individual,',') where ID = 3

            insert into @tab(field, descr, value)
            values(@field, @descr, @value)


        END

        SET @add_field = SUBSTRING(@add_field, LEN(@individual + ',') + 1,
                                                     LEN(@add_field))
    END
    ELSE
    BEGIN
        SET @individual = @add_field
        SET @add_field = NULL
        BEGIN
            SELECT @field=Data from dbo.rad_split('U' + @individual,',') where ID = 1
            SELECT @descr=Data from dbo.rad_split('U' + @individual,',') where ID = 2
            SELECT @value=Data from dbo.rad_split('U' + @individual,',') where ID = 3

            insert into @tab(field, descr, value)
            values(@field, @descr, @value)
        END
        --SELECT * from dbo.rad_split('U' + @individual,',')
    END
END
declare @tylda varchar(max)

Select @tylda = value from @tab where field = @field_name
--select @tylda
--select * from rad_split(@tylda,'~')


Declare @products varchar(4000) = @tylda
Declare @individual2 varchar(200) = null
Declare @sep varchar(20) = @code + '~'
DECLARE @ret_value varchar(100)

WHILE LEN(@products) > 0
BEGIN
    IF PATINDEX('%'+@sep+'%',@products) > 0
    BEGIN
        SET @individual2 = SUBSTRING(@products, 0, PATINDEX('%'+@sep+'%',@products))
        -- SELECT @individual2

        SET @products = SUBSTRING(@products, LEN(@individual2 + @sep) + 1,
                                                     LEN(@products))

    END
    ELSE
    BEGIN
        SET @individual2 = @products
        SET @products = NULL
        if CHARINDEX('~',@individual2) > 0 select @ret_value = ltrim(rtrim(left(@individual2,CHARINDEX('~',@individual2) - LEN(@sep)-1)))
        else select @ret_value = ltrim(rtrim(@individual2))
    END
END
    RETURN(@ret_value)
END

3 个答案:

答案 0 :(得分:1)

 1. split the long string by deliminator (,)
 2. each row that contain ~ split by deliminator ( )
 and store in a #table you name it 'USERTEXT(x)'
 3. do not store string with '%user%'

this is not exactly what you looking for but very close



AD~Advisory FOF~Fund of Funds SP~Structured Product FE~Feeder EX~Exclude CDV~CD Vehicle USERTEXT2
Investment type Advisory Capacity   
ND~Non Discretionary DI~Discretionary USERTEXT3
Investment type CD Input   
WI~with involvement NI~no involvement USERTEXT4
Investment type Investment Type CD   
PRIM~Primary SEC~Secondary GP~GP USERTEXT5

答案 1 :(得分:0)

使用此方法提取数据。

SET NOCOUNT ON
DECLARE @Test TABLE (value1 NVARCHAR(100), value2 NVARCHAR(100))

INSERT INTO @Test VALUES
    ('data1','data2'),
    (NULL,'data2'),
    ('data1','data2'),
    (NULL,NULL),
    ('data61',NULL),
    ('data31','data21'),
    ('data19','data20')

DECLARE @Data NVARCHAR(MAX)

SET @Data = ''

SELECT @Data = @Data +
    COALESCE(value1,'NULL') + ' ' +
    COALESCE(value2,'NULL') +CHAR(10)
FROM @Test
--Where x = y
--and z > 3

PRINT @Data

如果您有任何问题,请随时提出。

答案 2 :(得分:0)

减去所有类型的投资.....,'你将有这个字符串

USERTEXT1,
AD~Advisory FOF~Fund of Funds SP~Structured Product FE~Feeder EX~Exclude CDV~CD Vehicle 
USERTEXT2,
ND~Non Discretionary DI~Discretionary 
USERTEXT3,
WI~with involvement NI~no involvement 
USERTEXT4,
PRIM~Primary SEC~Secondary GP~GP 
USERTEXT5,
Current or Realised Investment   ,C~Current R~Realised 
USERTEXT6,
Identity U 
Client specific PICA Acc. Method   ,COST~Cost EQ~Equity USERBOOL1,
USERDATE1,
USERTEXT8,
Client specific Naming Convention   ,
USERTEXT9,
Client specific Property Number

从这里你需要很多想象力

如何提取usertext(x)并将其链接到适当的字段......