I have the following table:
Table1
USER_ID NAME AGE GENDER ZIP_CODE
1 John 33 M 01086
2 NULL 22 M 01247
3 Brown NULL F 01581
I want to add a bitmap as a column at the end.
USER_ID NAME AGE GENDER ZIP_CODE NULL_COLUMN
1 John 33 M 01086 00000
2 NULL 22 M 01247 01000
3 Brown NULL F 01581 00100
The logic is
If value other than NULL, assign 0
If value = NULL, assign 1
Also, if I make a column NULL, then it needs to update the NULL_COLUMN, in the following way:
Update table1 set name = NULL;
Table1
USER_ID NAME AGE GENDER ZIP_CODE NULL_COLUMN
1 NULL 33 M 01086 01000
2 NULL 22 M 01247 01000
3 NULL NULL F 01581 01100
Is there a way in Oracle SQL to create the NULL_COLUMN using the conditions and update it as the values become NULL?
You can use a generated column:
alter table table1
add null_map generated always as (nvl2(userJ_id, '1', '0') || nvl2(name, '1', '0') || nvl2(age, '1', '') || nvl2(gender, '1', '') || nvl2(zip_code, '1', '0'));
You could use a concat for NVL2
select USER_ID
,NAME
,AGE
,GENDE
,ZIP_CODE
, '0' || NVL2( NAME, '1', '0' ) ||
NVL2( AGE, '1', '0' ) ||
NVL2( GENDER, '1', '0' ) ||
NVL2( ZIP_CODE, '1', '0' ) NULL_COLUMN
from my_table
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.