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

Chandu Telu
Tera Guru
Tera Guru

HI Vikey, It's not a best practice to chnage thed filed type you need create a one more field and field name should be diffrent then current filed once field is created you can chnage the lable of field


Thanks Chandu for the quick reply.



  I have some another concern also. As there are lots of records in that field and it is used in reporting also. But now we are facing the issue as some users are able to submit the records as ten in place of 10.



Thanks


Hi Vikey, if you have any exiting data in that field don't delete that field you need to write an on change script to take only integer don't create any integer field and copy thi data it's not a good idea Ex: if you have any exsiting data in letters it won't copy to integer field


Mrudula6
Mega Guru

If you change data type now, it would wipe out all existing data in the field. Try creating a new field of type integer and transferring data to the new field. You can run a one-time script to copy the existing data. Later change the data type and re-copy the same back again.



Let me know if this approach works for you.