Insert and convert null to 0

I got a table with zero values ​​in it. I would like to copy this table to another and replace the null value from one column, for example 0. Is it possible that my target table does not accept null values ​​in this column

Insert into MyNewTable (A, B, C) select (AA, BB, CC) from MyOldTable

And CC may be null

thanks for any help

+3
source share
3 answers

Just use ISNULL()

Insert into MyNewTable (A, B, C) select (AA, BB, ISNULL(CC,0)) from MyOldTable
+6
source

IsNull(a, b) Coalesce(a, b, c, ...), IsNull "b", "a" null, Coalesce . , IsNull() ( ) "b" "a" , Coalesce , .

.. "a" int, IsNull(a, '1') , IsNull(a, 'hello') , Coalesce(a, 'hello') , , a null, , , ('hello') int, auotmatic a ( null) varchar.

+4

You can use the function coalescefor this:

INSERT INTO MyNewTable (A, B, C)
SELECT COALESCE(AA, 0), COALESCE(BB, 0), COALESCE(CC, 0) FROM MyOldTable
+1
source

Source: https://habr.com/ru/post/1791474/


All Articles