سؤال

In SSIS 2008 I have a variable called @[User::EANcode] It contains a string with a product eancode like '1234567891123'. The value is derived from a filename like'1234567891123.jpg' via a foreach loop.

However, sometimes the filenames contain an extra '_1', '_2' etc. at the end like '1234567891123_1.jpg' resulting in a value '1234567891123_1' in the EANcode variable.

This happens when there is more than one image for the same EANcode (product). The _N addition is always a number and it is always at the end of the name/string.

What is the expression to find/cath the '_1' (or_2 or_N etc) so you can store it in another variable called @[User::Addition]?

If there is no addition, the variable stays empty which is fine.

The reason I need to get this _N addition into a separate variable is that I later on need it to rename the filename but paste the addition back at the end.

Thanks!

هل كانت مفيدة؟

المحلول

I think you're looking for CHARINDEX() in conjunction with SUBSTRING(). With that, you can split off that _# to another variable like this (copy/pasta and execute to see. Play with the @temp1 variable to see the limitations of the code):

declare @temp1 varchar(20), @temp2 varchar(20)
set @temp1 = '1234567891123_12'

IF CHARINDEX('_', @temp1) > 1
set @temp2 = SUBSTRING(@temp1,CHARINDEX('_', @temp1),LEN(@temp1)-CHARINDEX('_',@temp1)+1)

select @temp1, @temp2

Hope it helps!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top