×

Good News Everybody!

The new search engine is LIVE!

Please report any problems to david (at) midrange.com.




Hi Alan,

In addition to the LEFT JOIN advice you're getting, which is likely needed
in your case, you may need or want to add something resembling this to the
mix:

select coalesce( nullif( FILE1.FIELD1, '' ), FILE2.FIELD1 ) as FIELD1
,coalesce( nullif( FILE1.FIELD2, '' ), FILE2.FIELD2 ) as FIELD2
from ...

Code like that delivers the first non-null, non-blank value found, from
left to right.

The same result can be accomplished with a CASE expression, but I tend to
prefer the above technique as it is less verbose.

I use code like that a lot in cases where a column is being delivered from
more than one source column in the database, and a choice needs to be made
to deliver the first non-blank column found. You could have two, or more,
non-blank values to choose from, in which case the order in which you code
the source columns will determine the results delivered.

Numeric columns would look like this:

select coalesce( nullif( FILE1.FIELD1, 0 ), FILE2.FIELD1 ) as FIELD1
,coalesce( nullif( FILE1.FIELD2, 0 ), FILE2.FIELD2 ) as FIELD2
from ...

Mike

From: MIDRANGE-L [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Rob
Berendt
Sent: Friday, June 30, 2017 2:00 PM
To: Midrange Systems Technical Discussion <midrange-l@xxxxxxxxxxxx>
Subject: Re: Using SQl to obtain a field value from one file or another

with T1 as (
select f1.field1, f2.field2 as col2
from f1 left inner join f2 on f1.field1 = f2.field1 union all select
f1.field1, f3.field2 as col2 from f1 left inner join f3 on f1.field1 =
f3.field2) select t1.field1, t1.col2 from t1 order by t1.field1

Rob Berendt
--
IBM Certified System Administrator - IBM i 6.1 Group Dekko Dept 1600 Mail
to: 2505 Dekko Drive
Garrett, IN 46738
Ship to: Dock 108
6928N 400E
Kendallville, IN 46755
http://www.dekko.com



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2026 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.