• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 156
  • Last Modified:

Coldfusion Data Manipulation

How do I extract a specific string from a given data.

For example:
I have a column name from a table labeled Name.  And this column contains the following data.

Name = Student: Senior | Jordan Kent | 2009

How do I extract just the name Jordan Kent?

Thanks

0
jramos74
Asked:
jramos74
  • 5
  • 2
  • 2
2 Solutions
 
duncancummingCommented:
Treat it as a list, delimited by " | "  What you want is the second list item.



<cfset name = "Student: Senior | Jordan Kent | 2009">
 
<cfset student = ListGetAt(name, 2, " | ")>

Open in new window

0
 
_agx_Commented:
You can treat it as a list, delimited by "|". Then extract the second element.  

BUT ... it is a bad idea to store multiple values  in a table column.  As you can see finding data is difficult and queries on this type of structure perform poorly.


<cfset NameValue = "Student: Senior | Jordan Kent | 2009">
<cfif listLen(NameValue, "|") gte 2>
	<cfset nameOnly = listGetAt(NameValue, 2, "|")>
	<cfoutput>
	  The nameOnly value is: #nameOnly#
	</cfoutput>
<cfelse>
	Unable to locate second element
</cfif>

Open in new window

0
 
_agx_Commented:
> <cfset student = ListGetAt(name, 2, " | ")>

That will not produce the results:  Jordan Kent
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
duncancummingCommented:
Right enough, I forget that the 3rd parameter of ListGetAt is a list of any delimiters, not a string equalling one delimiter.

This works instead:

<cfset student = Trim(ListGetAt(name, 2, "|"))>
0
 
_agx_Commented:
I would also  recommend checking the list length before using listGetAt(..). That way the code can exit gracefully if the value isn't what is expected. Otherwise, you just get an ugly exception message.
0
 
_agx_Commented:
jramos74,

Is there a reason you did not accept my answer, as it was correct and was also provided first?

0
 
jramos74Author Commented:
I meant to split the points because both examples compliment each other.  I do not how to re-open this question so i can split the points.
0
 
jramos74Author Commented:
Again, thank you for all your help.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now