14

I want it to know how to convert string field values to integer values in QGIS Dufour... there are numbers and strings in the same field, when I use toreal(string) it stops and it won´t create the field.

1 Answer 1

27

Update for QGIS 3.12 București: The function names are now to_int() and to_real(). If you want to convert Integers stored as QStrings to integers, try to_int() directly without the extra step of to_real() first.


I just tried to convert a string to an integer in QGIS Dufour with no issues. I had a text string that was 7 characters long. I did the following:

  • Create a new field (temp2) that is a whole number and holds 10 characters (longer than my source)
  • Field calculate temp2 with: toreal( "temp1" )
  • Field calculate temp3 with: toint( "temp1" )

Both examples worked and produced numbers from text. Make sure you only have numeric values in the source field. If you have any alpha values (a - z) or special characters (* # $ _ etc.) the conversion will not work. The source field must only contain numeric values (0 - 9)

5
  • Dear Ryan, that is the point, I have GPS data, some are numbers, others are descriptive string in the same field, I just want the numbers Commented Oct 21, 2013 at 19:40
  • Well, sometimes the answer is in your question... I will just select the values that I want to be converted! Works perfectly! Commented Oct 21, 2013 at 19:42
  • 1
    Carlos, are the GPS values uniform? What I mean is do they follow a pattern? In the example I had a text string that was hal1000000, hal1000001, hal1000002....What I did was to do a substr( "BLDGID" , 4, 7) This takes the source text field and chops out only the number values (starting at the 4th place and counting 7 values out). Here is a conversion to do it all in one. If you have number values in between letters you may need to be creative with the starting and ending numbers, or do it in two steps. Commented Oct 21, 2013 at 19:56
  • Ryan, I just sort by name then select the first ones (who has numbers) then use the field calculator and done!.. problema resuelto amigo. Gracias! Commented Oct 21, 2013 at 20:26
  • Glad you got it figured out. Commented Oct 21, 2013 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.