2

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

2
  • Alter table and add a default value to that column. Will work if column value not passed, but not if NULL is explicitly specified. Commented Feb 4, 2016 at 9:44
  • In addition to the answers given below, you could also handle NULL values in your app layer, and replace them there with appropriate substitutes. Commented Feb 4, 2016 at 9:48

4 Answers 4

2

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 )
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

enter image description here

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

enter image description here

Comments

0

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 )

3 Comments

It shows an error Error Code: 1582. Incorrect parameter count in the call to native function 'ISNULL'
try changing ISNULL to IFNULL. what database you're using?
i am using mysql only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.