Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
hervechambrin
Partner - Contributor
Partner - Contributor

How to split string and add the numbers

Hi All ,

I have a field with a string like  'Y1C170B15F100', a letter followed by a number between 1 and 999 and repeat  up to 20 times.

I need to parse it to load a sum of the integers, in that case above the result would be  '286'.

Thank you for your help.

1 Solution

Accepted Solutions
effinty2112
Master
Master

Hi Herves,

tews:

Load Capacity,

Evaluate(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Capacity,

'A','+'),

'B','+'),

'C','+'),

'D','+'),

'E','+'),

'F','+'),

'G','+'),

'H','+'),

'I','+'),

'J','+'),

'K','+'),

'L','+'),

'M','+'),

'N','+'),

'O','+'),

'P','+'),

'Q','+'),

'R','+'),

'S','+'),

'T','+'),

'U','+'),

'V','+'),

'W','+'),

'X','+'),

'Y','+'),

'Z','+')

)

as tews

;

LOAD * INLINE [

    Capacity

    A10B20

];

Gives a table

Capacity tews
A10B2030

View solution in original post

10 Replies
sunny_talwar

Not sure how you derived 286 from Y1C170B15F100. Would you be able to walk us through the logic?

hervechambrin
Partner - Contributor
Partner - Contributor
Author

Thank you for taking the time. I found a way to get the string as '1;170;15;100' , if you add theses numbers it will be 286. but now when I try to sum theses number I need theses 2 steps :

sub2:
LOAD
SubField('1;170;15;100',';') as sub2s
resident Capacity;

add:
LOAD
sum(sub2s) as addedfinal
resident sub2;

The following combination of sum and subfield  is not working, any idea why ?

sub:
LOAD
sum(SubField('1;170;15;100',';')) as subs
resident Capacity;

Thanks

sunny_talwar

May be this:

Table:

LOAD Sum(subs) as subs;

LOAD SubField(Field, ';') as subs;

LOAD * Inline [

Field

1;170;15;100

];

effinty2112
Master
Master

Hi Herve,

This one is great fun! Try this expression

=

num(

$(=

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

'$(vString)',

'A','+'),

'B','+'),

'C','+'),

'D','+'),

'E','+'),

'F','+'),

'G','+'),

'H','+'),

'I','+'),

'J','+'),

'K','+'),

'L','+'),

'M','+'),

'N','+'),

'O','+'),

'P','+'),

'Q','+'),

'R','+'),

'S','+'),

'T','+'),

'U','+'),

'V','+'),

'W','+'),

'X','+'),

'Y','+'),

'Z','+')))

This will return the number you're looking for for whatever string you set vString equal to.

vStringY1C170B15F100
vSum286
hervechambrin
Partner - Contributor
Partner - Contributor
Author

Thank you Andrew, is it possible to get the same result in a LOAD script, I tried a lot of different syntax but always get the same result ? The following  returns '+10+20'

tew:
LOAD
num#(Replace(Replace(Capacity,'A','+'),'B','+')) as tews
INLINE [
Capacity
A10B20
]
;

sunny_talwar

You were able to change letters into semi-colons already right? Have you tried this?

Table:

LOAD Sum(subs) as subs;

LOAD SubField(Field, ';') as subs;

LOAD * Inline [

Field

1;170;15;100

];

hervechambrin
Partner - Contributor
Partner - Contributor
Author

Hello Sunny, I tried but it didn't work  in my case I need to do it within 1 LOAD statement while looping thru multiples QVDs.

effinty2112
Master
Master

Hi Herves,

tews:

Load Capacity,

Evaluate(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Replace(

Capacity,

'A','+'),

'B','+'),

'C','+'),

'D','+'),

'E','+'),

'F','+'),

'G','+'),

'H','+'),

'I','+'),

'J','+'),

'K','+'),

'L','+'),

'M','+'),

'N','+'),

'O','+'),

'P','+'),

'Q','+'),

'R','+'),

'S','+'),

'T','+'),

'U','+'),

'V','+'),

'W','+'),

'X','+'),

'Y','+'),

'Z','+')

)

as tews

;

LOAD * INLINE [

    Capacity

    A10B20

];

Gives a table

Capacity tews
A10B2030
hervechambrin
Partner - Contributor
Partner - Contributor
Author

Thank you All, this is working for me.