Bizarre Behaviour in SharePoint Calculated Columns

Calculated columns within SharePoint come in useful and broadly follow the syntax which is used in Excel functions. But sometimes they exhibit strange behavior. Take the following case. I have a list with items which can be linked to one another and needed to create a view which would display all items which are linked together. This could be done using a formula in a calculated field, except that a hyperlink field cannot be included in a calculated column formula.

So I wrote a little workflow to copy the contents of my hyperlink field to another text field (called ParentLink) and then run my calculated column against this field using the following formula:

ParentLink is my text field and so the formula looks to see if it contains a hyperlink, and if yes then it will extract the ID number at the end of the URL. If the key text “http” is not found then it enters the ID of the item. The formula seems to work, correctly displaying the ID numbers of parent items and any child items that are linked to them. However, if an item is edited and closed then, if there is no link, the ID number is not retained but is set to 0. This happens whether the format of the calculated column is text or number.

Currently, my workaround is to populate the ParentLink field either with a hyperlink if it exists, or with the ID of the item if it does not. Then, rather than using the ID field, I just use the ParentLink field as follows: