sql - Updating one column based on the value of another column -
i have table named vendor, within table have column called accountterms shows value (i.e. 0, 1, 2, 3) , on. have column want use (ularageing
) in order reflect meaning of value, such as:
0: current 1: 30 days 2: 60 days
and on...
what need script @ value in accountterms , update ularageing
show word value shown above. how do this?
i going try explain in simple manner as possible it's easy understand :
let's assume, have table vendor
setup this:
create table vendor (accountterms int, ularageing varchar(50));
and, insert sample values both columns in vendor
table:
insert vendor values (0,'test'), (1,'test1'), (2,'test2');
next, write update statement update ularageing
column based on values in accountterms
column in same table:
update vendor set ularageing = (case when accountterms = 0 'current' when accountterms = 1 '30 days' when accountterms = 2 '60 days' end);
case when
similar using if..else
statement in other programming languages. so, here updating existing ularageing
value different string value based on condition in case when statement. so, e.g. if accountterms = 0
update value ularageing
`current' , forth.
to check if above statement worked correctly, need run update statement above , select table again:
select * vendor;
result:
+--------------+-----------------+ | accountterms | ularageing | +--------------+-----------------+ | 0 | current | | 1 | 30 days | | 2 | 60 days | +--------------+-----------------+
Comments
Post a Comment