Concatenate fields with if else statement
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi,
I have a list of names that I want to concatenate them with a seperator (comma) (please see below), constrained to following criteria
1- Remove all dots spaces from middle initial
2-If there is no middle initial, it should look like Adam, Thomas not Adam, Thomas, ---> No comma at the end
3-Same procedure applies to first Name . If there is no FIrst Name, just include last name with no comma
Expect outcome should look like below. Not sure If i can do it with formula function by using if else (or something else) statement. Any help is appreciated
First Name | Last Name | Middle Initial | Output |
Tom | Jack | D.R | Tom,Jack,DR |
Skura | Daniel | Skura,Daniel | |
Tim | Treak | F | Tim,Treak |
Tim | Howard | Tim,Howard | |
Simmons | Simmons |
Solved! Go to Solution.
- Labels:
- Expression
- Parse
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Probably the simplest way is to use the Data Cleansing tool to remove the spaces and . etc as initial clean up step.
You can then use a formula tool to make the concatenated string:
[Last Name] + IIF(IsNull([First Name]) || [First Name] = '', '', ', ' + [First Name]) + IIF(IsNull([Middle Initial]) || [Middle Initial] = '', '', ', ' + [Middle Initial])
You can do this in one step using the REGEX_Replace function to clean up:
REGEX_Replace([Last Name], '[^A-Za-z]', '') + IIF(IsNull([First Name]) || REGEX_Replace([First Name], '[^A-Za-z]', '') = '', '', ', ' + REGEX_Replace([First Name], '[^A-Za-z]', '')) + IIF(IsNull([Middle Initial]) || REGEX_Replace([Middle Initial], '[^A-Za-z]', '') = '', '', ', ' + REGEX_Replace([Middle Initial], '[^A-Za-z]', ''))
Sample attached
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Thanks for sharing the + SIGN tip for concatenation. I also appreciate the REGEX_WITH REPLACE. The second item is very useful and nicely explained.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Parse Error at char(153): Malformed IF statement (Expression #2)
[LN_ID] + "-" + [GL_MATRIX] + "-" +
IF [HfsFlag]="Y"
then "HFS"
elseif [HfsFlag]="N"
then "HFI" else ""
+
IF [FvoFlag] = "Y" THEN "-FVO"
ELSE ""
ENDIF
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Abhay,
This works and thanks for your help. I removed the pipes ( || ) from my Teradata SQL and used the +.
David
