How to change data type of a field?

vikey1234
Kilo Contributor

Hi Folks,

        We have made a field as string by mistake. Now we need to change it to integer. We have stored only integer value in the field also but in string format.

Is there any way to convert the data type as integer.

Thanks in advance.

Vikash

1 ACCEPTED SOLUTION

HI Vikey,




Fix script sample:




var gr = new GlideRecord("TABLENAME");


gr.addNotNullQuery('INTEGERFIELDNAME');


while(gr.next()){


gr.STRINGFIELDNAME = gr.INTEGERFIELDNAME.toString();


gr.update();


}




post me your feedback




Please Hit ✅Correct, ��Helpful, or ��Like depending on the impact of the response


Have a lovely day ahead




Regards,


Divya Mishra


View solution in original post

19 REPLIES 19

divya mishra
Tera Guru

Hi Vikey,



Only possible way would be:


1. Create a string field.


2. write a fix script to copy the existing values from integer field to string field.


3. Do not forget to do .toString() to the values in integer.


4. Delete the integer field.



post me your feedback


Please Hit ✅Correct, ��Helpful, or ��Like depending on the impact of the response


Have a lovely day ahead




Regards,


Divya Mishra


HI Vikey,




Fix script sample:




var gr = new GlideRecord("TABLENAME");


gr.addNotNullQuery('INTEGERFIELDNAME');


while(gr.next()){


gr.STRINGFIELDNAME = gr.INTEGERFIELDNAME.toString();


gr.update();


}




post me your feedback




Please Hit ✅Correct, ��Helpful, or ��Like depending on the impact of the response


Have a lovely day ahead




Regards,


Divya Mishra


Hi Vikey,



Didn't you say you were converting a string field to an integer? The answer you marked correct is converting an integer to a string. If it's the other way around, then this is the solution:



var gr = new GlideRecord("TABLENAME");


gr.addNotNullQuery('STRINGFIELDNAME');


while(gr.next()){


        var intValue = parseInt(gr.getValue('STRINGFIELDNAME'), 10);


        gr.setValue('INTEGERFIELDNAME', intValue);


        gr.update();


}


Hey Chuck,



I have mentioned my code as a "sample code" which Vikey can modify and proceed. Cheers



Regards,


Divya


The challenge I have found in the community is that most people just copy/paste the script you include and expect to work unless you either put in the specifics, "This is sample untested code" or "Please replace with proper field and table names for your use case", etc. Even then it sometimes fails.



The more specific to their solution, the better. That being said, you don't always know and have to provide specific instructions. In this case his original request to convert a string to an integer



We have made a field as string by mistake. Now we need to change it to integer.


I thought it worth noting that your script did the reverse. In either case, I'm glad he got it resolved.