1

I have an example table below:

+----+----+------+
|var1|var2|merged|
+----+----+------+
|a   |b   |a     |
+----+----+------+
|    |c   |c     |
+----+----+------+
|    |x   |x     |
+----+----+------+
|c   |d   |c     |
+----+----+------+

I want to merge var1 and var2's values with the rule that whenever var1 has a value, it should be picked over var2.


My code below does not achieve it and I am at a loss on how to implement this.

SELECT id, var1 & var2 AS merged
FROM myTable

All this code does is concatenated var1 and var2's data which is not I want. The one I asked before is not working as desired anymore.

+----+----+------+
|var1|var2|merged|
+----+----+------+
|a   |b   |ab    |
+----+----+------+
|    |c   |c     |
+----+----+------+
|    |x   |x     |
+----+----+------+
|c   |d   |cd    |
+----+----+------+

The one below from this question does not take into account lone values :(


SELECT ID, var1, var2, 
  SWITCH (        
    var1 IS NOT NULL, var1,     
    var2 IS NOT NULL, var2,   
  ) AS merged
FROM myTable;
+----+----+------+
|var1|var2|merged|
+----+----+------+
|a   |b   |a     |
+----+----+------+
|    |c   |      |
+----+----+------+
|    |x   |      |
+----+----+------+
|c   |d   |c     |
+----+----+------+

UPDATE

After using the methods provided in the answers, I have seen some of my records achieve what I presented in the first table above. But after looking through the other rows I managed to find ones that were skipped, as presented in the third table.

Below is the code I use and screenshots of the results.

SELECT ID, var1, var2, 

      # method 1
      SWITCH (        
        var1 IS NOT NULL, var1,     
        var2 IS NOT NULL, var2
      ) AS switchResult,

      # method 2
      Nz(var1, var2) AS NzResult,

      # method 3
      Iif(var1 IS NULL, var2, var1) AS IifResult

FROM myTable;

The results here are what I wanted:

Note: ID, var1, var2, switchResult, NzResult, IifResult are the column names for guide

enter image description here

But as I search for (Blanks) there appears to be failed rows:

enter image description here

It seems that var1's values if var2's missing return correct queries but when var2 does have a value and var1 does not, some rows do not return correct queries.

Does anyone know why this happens? I already checked if var1 and var2 contains whitespace and they do not.

Community
  • 1
  • 1

2 Answers2

1

try this:

SELECT var1, var2, COALESCE(var1, var2) AS merged
  FROM myTable;
MauriDev
  • 445
  • 2
  • 8
1

You want var1 when it is not Null, and otherwise you want var2. You can use Nz to get that.

SELECT var1, var2, Nz(var1, var2) AS  merged
FROM myTable;

If you will be running the query from outside an Access session, the Nz function will not be available. In that case, you can use an IIf expression.

SELECT var1, var2, IIf(var1 Is Null, var2, var1) AS  merged
FROM myTable;

If var1 may contain zero-length strings (""), and you want those treated the same as Null --- meaning return var2 in that situation --- use this query ...

SELECT var1, var2, IIf(Len(var1 & "") = 0, var2, var1) AS  merged
FROM myTable;
HansUp
  • 95,961
  • 11
  • 77
  • 135
  • Hello again! I just found out that _some_ of my records' `var2` values are not put in the `merge` field even though `var1` is empty (no whitespace too). Do you know why this happens? –  Jan 15 '14 at 02:39
  • 1
    Does the table design all zero-length strings in `var1`? Check the table in Design View ... the property is called *Allow Zero Length* in the General tab of the lower panel. – HansUp Jan 15 '14 at 03:22
  • Yes, `var1`'s _Allow Zero Length_ is set to _Yes_. –  Jan 15 '14 at 03:24
  • 1
    Does this query return any rows? `SELECT * FROM myTable WHERE Len(var1) = 0;` If so, are those the same rows which don't give what you expect in the merge field? – HansUp Jan 15 '14 at 03:26
  • Yes, it returns rows and these are the same rows with the problem. Do I have to redo my database? –  Jan 15 '14 at 03:29
  • 1
    No, you don't have to; I'll revise the answer to cope with zero-lenth strings in `var1`. But I believe it is bad design to permit both Null and zero-length strings in a text field. Permit one only; not both. Allowing both leads to the type of confusion we're dealing with now. – HansUp Jan 15 '14 at 03:44
  • Thank you very much. I actually just imported a CSV and never thought of this occurrence. I'll take note of it in the future. –  Jan 15 '14 at 03:45
  • 1
    OK, after importing the CSV, you can substitute Null for zero-length strings in `var1` ... `UPDATE myTable SET var1 = Null WHERE Len(var1) = 0;` – HansUp Jan 15 '14 at 03:49