I need to write an SQL query to take either from one of two column’s data(based on whichever is available and not null) and append it to a static text into another column within the same table.
Can anybody tell me how to write this?
Example Data :
ID Type Barcode Serial No Location
1 Test ABCD 1234 LOC1
2 Test EFGH NULL LOC2
3 Test NULL 5678 LOC3
4 Test NULL NULL LOC1
Final Data Reqd in Format
ID Type Barcode Serial No Location
1 Test ABCD 1234 LOC1-ABCD (Append barcode if its not null)
2 Test EFGH NULL LOC2-EFGH (Append barcode if its not null)
3 Test NULL 5678 LOC3-5678 (Append serial no since barcode is null)
4 Test NULL NULL LOC1 (Both r Null keep loc as it is)
Please help me on it….really stuck on this 🙁
Database is Oracle 10.
Supra
Update 1 :
Thanks a lot Marco for your help….the location field only needs to updated in the table after appending the barcode/serial no from the same table into location field. Your edited query is not working :(…please let me know if I need to give some more info/data.
Final Update :
Shesek’s Answer worked perfect :D…you are the man :)…Thanks a ton 😀
According to your comment on the other answer,
If you want to append this to the current value of FULL_ADDRESS, as I understand from the original question,
COALESCE()returns the first non-NULL argument you pass to it. See Oracle’s manual page on it.Just as a general FIY,
NVM()that was suggested by another answers is the old Oracle-specific version ofCOALESCE(), which works kinda the same – but it only supports two arguments and evaluates the second argument even if the first one is non-null (or in other words, its not short-circuit evaluated). Generally, it should be avoided and the standardCOALESCEshould be used instead, unless you explicitly need to evaluate all the arguments even when there’s no need for it.