Wesley Bakker

Interesting things I encounter doing my job...

Sponsors

News

Wesley Bakker
motion10
Rivium Quadrant 151
2909 LC Capelle aan den IJssel
Region of Rotterdam
The Netherlands
Phone: +31 10 2351035

(feel free to chat with me)
 

Add to Technorati Favorites

SharePoint Calculated Field from string to number

Every now and then you run into unexpected SharePoint behavior. The Title field of an item is a 'single line of text' field. In my specific case however this Title field contained a file number. Not a problem at all, BUT you get a really strange sort order when you start to sort numbers as strings. File number 1000 comes before file number 9. This is however expected behavior.

The client wanted to be able to sort the items in a numeric way so I simply created a calculated columns of type number with this expression "=[Title]". Easy peasy right? And that's where the unexpected behavior came along. The field indeed got the value of the Title field, but if I sorted the list by this field, it behaved exactly as if the field contained a single line of text. So again, 1000 comes before 9. Checked if I indeed set the calculated field to be a number and well, I did. The value simply wasn't treated as a number but as text. Strange...

So I started digging in my development experience and I decided to make a little change to the expression. I changed the expression to "=[Title] + 0". And what do you know? The calculated field now indeed returned a number and was treated like a number. Sorting by the field now produced the expected behavior. File number 9 now comes before file number 10000.

Cheers,

Wes

Comments

SharePoint Calculated Field from string to number - Wesley Bakker said:

Pingback from  SharePoint Calculated Field from string to number - Wesley Bakker

# August 8, 2010 4:08 PM

JS said:

I have a single line of test field called "Project ID", I am trying to create a calculated number column called PrjId. I tried => "[Project ID] + 0" for the calculate field value but it did not work.

# September 19, 2010 7:00 PM

Kam said:

Perhaps you are not using the right syntax!!

"=[Project ID] + 0"

are you missing equals sign..?

Additionally ensure the site column name (field) indeed has space between Project and ID...in SharePoint you could easily be misled by the Display name and the actual name given/created for the field!

# October 20, 2010 2:12 PM

QuyDA said:

Hi Kam!

I did used your syntax.

But my Calculated Field is combines the two strings and separated by a comma.

Ex:

15. ChapterName

2.  ChapterName

This column is sort numbers as strings.

Can you help me sort order correct? Please!

# November 30, 2010 9:34 PM

TB said:

Thanks Wesley. Worked great for me. I had noticed the Text issue but never would have thought of +0 as a resolution.

# July 28, 2011 3:58 PM

Dan said:

Worked great for me.  Thank you!

# September 15, 2011 4:55 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)