Skip to content
Advertisement

Change datatype of a whole column from string to integer in SQLite?

I’m pretty new to SQL. I’m practicing on a dataset I downloaded, unfortunately some int type columns became varchar when I imported them. I need to change them back into int.

I tried to create a temp table, create the correct schema for the table with appropriate datatype for all columns, then dump everything back. I got an int column with a mixture of int and empty strings. How do I convert empty strings to zeros? Thank you.

Advertisement

Answer

With an UPDATE statement like this:

UPDATE tablename
SET columnname = 0
WHERE columnname is null or trim(columnname) = ''

Remove columnname is null if you don’t have nulls but empty strings.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement