Question
· Apr 30, 2021

How can I add two values in a segment field separated by ~

I am updating the PV1 segment with values from the database but when I get two values back I dont know how to separate them with a tilde and insert. Any advice for me on that?

so if I get two values returned

 

my PV1.7 segment should be :  

My current code is as below: It works fine for one value, but when there are more than 2 values returned I need to find a way to include it

Set res=##class(%ResultSet).%New("%DynamicQueryGW:SQLGW")

Set sc=res.Prepare(sqlstring,,conn)

Set sc=res.Execute()

while res.Next()
    {
     RET = res.GetData(2)
}
    
    sc=res.Close()
    
    Set sc=conn.Disconnect()

//Modify the segment   
//create a copy of the request
newREQ = pRequest.%ConstructClone()

//now we use the copy for manipulation
PV1 = newREQ.FindSegment("PV1")
set res = PV1.SetValueAt(RET,3.1)
Quit request

Product version: Ensemble 2018.1
Discussion (5)1
Log in or sign up to continue

Try replacing your while loop with this:

//set RET to a blank string to start to avoid issues with the first concatenation inside the loop
s RET = ""
while res.Next()
    {
     //Append a ~ and the value to RET
     s RET = RET_"~"_ res.GetData(2)
}
//The way we did this, RET will now start with a ~, which we'll want to remove
//This will look at RET, replace tildes with nothing, starting at the beginning, and only making one replacement
$REPLACE(RET,"~","",1,1)

//having done that, RET should now be, "description 1~description 2"

You should need only to iterate over the result set and populate the repetition of the PV1 field. Note that you reference PV1.7 (attending provider) in your narrative, but your code references PV1.3 (assigned patient location), so adjust the below as needed to identify the proper field:

//create a copy of the request
Set newREQ = pRequest.%ConstructClone()
//now we use the copy for manipulation
Set res=##class(%ResultSet).%New("%DynamicQueryGW:SQLGW")
Set sc=res.Prepare(sqlstring,,conn)
Set sc=res.Execute()
// create a repetition counter
Set cnt=1
While res.Next()
{
    Set RET = res.GetData(2)
    //Modify the segment
    //Reference the correct repetition for the current result set member
    set res = newREQ.SetValueAt(RET,"PV1:3("_cnt_")")
    //Increment the repetition counter
    Set cnt=cnt+1
}
Set sc=res.Close()
Set sc=conn.Disconnect()

The appropriate repetition delimiters as defined within the message schema (in this case "~") are automatically inserted.

Note that the SetValueAt() method has been modified with the assumption that you're using a consistent HL7 schema across all messages and know the correct path to the PV1 field in question. In that case the FindSegment() method should not be necessary.