简体   繁体   中英

how to replace NULL value during insertion in sql

my table looks like this

+--------+--------+--------------+--------------+
| CPI_id | Weight | score_100_UB | score_100_LB |
+--------+--------+--------------+--------------+
|    1.1 |     10 |          100 |           90 |
+--------+--------+--------------+--------------+

while executing the insert query the table should look like

+--------+--------+--------------+--------------+
| CPI_id | Weight | score_100_UB | score_100_LB |
+--------+--------+--------------+--------------+
|    1.1 |     10 |          100 |           90 |
|    5.5 |     10 |         NULL |           93 |
+--------+--------+--------------+--------------+

but NULL values should be replaced by 100. I also tried using trigger.I couldn't get.

thanks in advance

在此处输入图片说明

 select E.emp_name as Employee, ISNULL( M.emp_name,'No Manager') as Manager from Employee_Test_salary4 E left join Employee_Test_salary4 M on E.report_manager = M.report_manager or select E.emp_name as Employee, COALESCE( M.emp_name,'No Manager') as Manager from Employee_Test_salary4 E left join Employee_Test_salary4 M on E.report_manager = M.report_manager or select E.emp_name as Employee, CASE WHEN M.emp_name IS NULL THEN 'No Manager' ELSE M.emp_name END as Manager from Employee_Test_salary4 E left join Employee_Test_salary4 M on E.report_manager = M.report_manager 

在此处输入图片说明

Alter your table and set the field score_100_UB to have some default value like below

 ALTER TABLE t1 MODIFY score_100_UB INT UNSIGNED DEFAULT 100;

After this, whenever you try to insert a NULL value in this column, it will be replaced by 100

if you're using a query then use ISNULL() function

insert into table values (CPI_id , Weight ,ISNULL(score_100_UB ,100), score_100_LB )

For MySQL use:

insert into table values (CPI_id , Weight ,IFNULL(score_100_UB ,100), score_100_LB )

or:

insert into table values (CPI_id , Weight ,COALESCE(score_100_UB ,100), score_100_LB )

SQL Server:

insert into table values (CPI_id , Weight ,ISNULL(score_100_UB ,100), score_100_LB )

Oracle:

insert into table values (CPI_id , Weight ,NVL(score_100_UB ,100), score_100_LB )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM