Updating a column depending on the value set in another column in the same row

Multi tool use
Multi tool use


Updating a column depending on the value set in another column in the same row



Got a table containing a FLAG column (any char) and two others fields ESD and TD (both date).
The FLAG column must be preceded by either the char E if ESD isn't null for that row, or followed by T if TD isn't null for that row.


FLAG


ESD


TD


FLAG


E


ESD


T


TD



Note


FLAG


ESD


TD


ESD


TD



Example



Before


+-------+-------------+-------------+
| FLAG | ESD | TD |
+-------+-------------+-------------+
| V | 2018/05/01 | (null) |
| D | (null) | (null) |
| V | (null) | (null) |
| V | (null) | 2018/06/31 |
| V | (null) | (null) |
| D | 2018/01/01 | 2018/08/31 |
+-------+-------------+-------------+



After


+-------+-------------+-------------+
| FLAG | ESD | TD |
+-------+-------------+-------------+
| EV | 2018/05/01 | (null) |
| D | (null) | (null) |
| V | (null) | (null) |
| VT | (null) | 2018/06/31 |
| V | (null) | (null) |
| EDT | 2018/01/01 | 2018/08/31 |
+-------+-------------+-------------+



Question



What is the easiest way to get this result in SQL or PL/SQL ?





Are you really trying to update the value stored in the table; or just how it appears in the result set when you query the table?
– Alex Poole
Jul 3 at 9:07





I'm trying to update it in the table
– Hey StackExchange
Jul 3 at 9:32




3 Answers
3



You can use concatenation and two case expressions:


case when esd is not null then 'E' end
|| flag
|| case when td is not null then 'T' end as flag



Demo with your data in a CTE (correcting as June doesn't have 31 days):


with your_table (FLAG, ESD, TD) as (
select 'V', date '2018-05-01', null from dual
union all select 'D', null, null from dual
union all select 'V', null, null from dual
union all select 'V', null, date '2018-06-30' from dual
union all select 'V', null, null from dual
union all select 'D', date '2018-01-01', date '2018-08-31' from dual
)
select
case when esd is not null then 'E' end
|| flag
|| case when td is not null then 'T' end as flag,
esd,
td
from your_table;

FLAG ESD TD
---- ---------- ----------------------------
EV 2018-05-01
D
V
VT 2018-06-30
V
EDT 2018-01-01 2018-08-31



If you really want to update the value in the table then you can use the same thing in an update statement:


update your_table
set flag =
case when esd is not null then 'E' end
|| flag
|| case when td is not null then 'T' end;



but then the flag will be wrong as soon as a null date column is populated, and will be harder to recalculate when you need to. You should either just adjust this as you query it, optionally via a view; or add a virtual column that holds the generated value separately.





Thanks works. Marked answered and up voted!
– Hey StackExchange
Jul 3 at 9:52



Seems like a simple update using CASE statements?


CASE


UPDATE [table] SET FLAG = CASE WHEN ESD IS NOT NULL THEN 'E' ELSE '' END || FLAG || CASE WHEN TD IS NOT NULL THEN 'T' ELSE '' END;





Work. Thanks, up voted
– Hey StackExchange
Jul 3 at 9:51



you are updating a data here and you have to do them one by one as follows. If your values are duplicate, like as I see in your table 'D', 'V' and 'VT' once you execute the command displayed below it will update all values defined in the where clause. you wanted to update the ESD and TD column when notnull.



update (tablename)
set Flag = 'ED'
where ESD = 'notnull'



update (tablename)
set Flag = 'TVT'
where TD = 'notnull'



update (tablename)
set Flag = 'ExT'
where TD = 'Notnull' and ESD = 'notnull'





This isn't based on whether there are non-null values in the two date columns, which is the point of the question (and in the title). Even as hard-coded updates, which isn't what the OP wants, this doesn't get the result they wanted.
– Alex Poole
Jul 3 at 9:10






as I can see he wanted to update the values in the column Flag where the other columns which hold of datetime values (ESD and TD) are notnull
– kebrewesson Gelaye
Jul 3 at 9:23





@kebrewessonGelaye that's right. Thanks for your reply. But your answer would work only for know FLAG value (not reusing the existing one).
– Hey StackExchange
Jul 3 at 9:35






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Hatnke3,l BX C4Zc n OiouX Pn5N9SZr UYBalP42 EfAYo,jkenRVtnp YHcnjLVKhWRQHDRgKb DfhQdYqgRGJgBvLq,g5mHz,alIDwUr
Zkueiv77JjuFw09e bxj2ocLxjV,qX1Y9hRoz,m6NuZqc6fsPbLFT

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications