Skip to main content
Announcements
Accelerate Your Success: Fuel your data and AI journey with the right services, delivered by our experts. Learn More
cancel
Showing results for 
Search instead for 
Did you mean: 
chriskan
Contributor III
Contributor III

Remove all character/symbols after comma

Hello, 

I have a column filled with strings similar to 'Mercury, Venus, Saturn, Neptune', 'Mars', 'Dwarf Planet, Pluto' etc.

How do I remove everything after and including the first comma to result in 'Mercury', 'Mars', 'Dwarf Planet'?

Thanks

Labels (1)
1 Solution

Accepted Solutions
chriskan
Contributor III
Contributor III
Author

Tried a few functions and turns out this works best:

Master Items > Measures > Create new > Field > expression editor > SubField(Concat(Distinct FieldName, ','), ',', 1)

View solution in original post

15 Replies
therealdees
Creator III
Creator III

Hi, try this:

 

You could load the correct values in the loader:

SubField(yourfield, ',', 1) as yournewfield,

 

Or you could try in the chart expressions:

SubField(yourfield, ',', 1)

 

 

For further information, please check this link:

https://help.qlik.com/en-US/qlikview/May2023/Subsystems/Client/Content/QV_QlikView/Scripting/StringF...

 

marcus_sommer

Maybe with: subfield(YourString, ''',', 1)

therealdees
Creator III
Creator III

This function is valid for chart expressions also. You could use the very same expression that I posted below:

 

SubField(yourfield, ',', 1)

 

Subfield will evaluate the string and look for the delimiter (in this case is ",")

chriskan
Contributor III
Contributor III
Author

I should maybe mention that I'm adding this substring expression inside a formula as displayed below:

=Concat(Distinct [=SubField(FieldName, ',', 1)], ',')

Unfortunately, the above solution does not work. Any suggestions?

 

Or
MVP
MVP

Lots of responses, but everyone seems to have gone the subfield() route...

Perhaps:

Left(Field, Index(Field,',')-1)

chriskan
Contributor III
Contributor III
Author

This solution causes my column to become blank. Interestingly, using:

Left(Field, Index(Field,',')+1)

seems to turn my strings from:

'Mercury, Venus, Saturn, Neptune', 'Mars', 'Dwarf Planet, Pluto'

to:

'M, V, S, N', 'M', 'D, P'

Any ideas as to why your solution does not work as something odd seems to be going on?

Or
MVP
MVP

Index(Field, ',') should return the position of the first comma in your string, e.g. 'Hello, World' should return 6.

You want everything left of that position but not including that position, hence left(Field, Index(Field,',')-1)

I suppose that doesn't pick up on anything which has no comma at all, in which case Index() will return 0, so:

If(Index(Field,',')=0,Field, left(Field, Index(Field,',')-1))

marcus_sommer

It's not quite clear for me what you are trying to do. My suggestion will work in regard to your example.

If this nested string comes from an external source I recommend to split the parts already within the script. This could be done with the shown subfield() with several subfield() loops within preceding loads by using iterno() as iterator as well as position identifier within the dataset.

If you are creating this string I suggest to use different delimiter for each level - which would remove the need to combine quotes and delimiter to pick the wanted sub-strings.

chriskan
Contributor III
Contributor III
Author

I'm not sure that's the problem. I tested the expression:

Left(Field, 3)

which returned this:

Original: 'Mercury, Venus, Saturn, Neptune', 'Mars', 'Dwarf Planet, Pluto'

Result:  'Mer, Ven, Sat, Nep', 'Mar', 'Dwa, Plu'

so the expression seems to loop each time it hits a comma.

Furthermore, I tested your if statement which made no change to my data. Any further suggestions?