简体   繁体   English

如果不满足条件,派生列表达式SSIS如何忽略表达式?

[英]Derived column expression SSIS how to ignore expression if condition is not met?

I currently have the below expressions which splits up a string of text based on a hyphen (-) so that it can be mapped to different fields using Derived column transformation in SSIS/SSDT. 我目前有以下表达式,它们基于连字符(-)分割文本字符串,以便可以使用SSIS / SSDT中的派生列转换将其映射到不同的字段。 However I have noticed that some data does meet the criteria eg. 但是我注意到一些数据确实符合标准,例如。 some data does not have any hyphens or just one. 有些数据没有连字符或只有一个。

What could I add to the statement in order to ignore if condition is not met? 如果不满足条件,我可以在语句中添加什么以忽略?

my current expressions are: 我当前的表达式是:

SUBSTRING(FIELDNAME,
FINDSTRING(FIELDNAME,"-",1) + 1,
(FINDSTRING(FIELDNAME,"-",2) - FINDSTRING(FIELDNAME,"-",1)) - 1)

and

SUBSTRING(FIELDNAME,1,FINDSTRING(FIELDNAME,"-",1) - 1)

Thanks 谢谢

This function currently return 9 positions, but it is easy to expand or reduce. 该函数当前返回9个位置,但是很容易扩展或缩小。

Notice that we are appending a '-' to the string to be processed 请注意,我们在要处理的字符串后附加了“-”

Declare @Sample table (FIELDNAME varchar(200))
Insert Into @Sample values
('ABC-123-XYZ'),
('Some-Text'),
('NoHyphen')

Select *
 From @Sample A
 Cross Apply [dbo].[udf-Str-Parse-Row](A.FIELDNAME+'-','-') B

Returns 返回

FIELDNAME   Pos1     Pos2       Pos3    Pos4    Pos5    Pos6    Pos7    Pos8    Pos9
ABC-123-XYZ ABC      123        XYZ     NULL    NULL    NULL    NULL    NULL
Some-Text   Some     Text       NULL    NULL    NULL    NULL    NULL    NULL
NoHyphen    NoHyphen NULL       NULL    NULL    NULL    NULL    NULL    NULL

OR with just a String 或者只用一个字符串

Select * from [dbo].[udf-Str-Parse-Row]('ABC-123-XYZ'+'-','-')

Returns 返回

Pos1    Pos2    Pos3    Pos4    Pos5    Pos6    Pos7    Pos8    Pos9
ABC     123     XYZ     NULL    NULL    NULL    NULL    NULL    NULL

The UDF if needed UDF(如果需要)

CREATE FUNCTION [dbo].[udf-Str-Parse-Row] (@String varchar(max),@Delimiter varchar(10))
Returns Table 
As
Return (
    Select Pos1 = xDim.value('/x[1]','varchar(max)')
          ,Pos2 = xDim.value('/x[2]','varchar(max)')
          ,Pos3 = xDim.value('/x[3]','varchar(max)')
          ,Pos4 = xDim.value('/x[4]','varchar(max)')
          ,Pos5 = xDim.value('/x[5]','varchar(max)')
          ,Pos6 = xDim.value('/x[6]','varchar(max)')
          ,Pos7 = xDim.value('/x[7]','varchar(max)')
          ,Pos8 = xDim.value('/x[8]','varchar(max)')
          ,Pos9 = xDim.value('/x[9]','varchar(max)')
     From (Select Cast('<x>' + Replace(@String,@Delimiter,'</x><x>')+'</x>' as XML) as xDim) A
)
--Select * from [dbo].[udf-Str-Parse-Row]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse-Row]('John Cappelletti',' ')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM