问题
I have multiple MS Access fields and I need to combine most of them with a condition. I'm still a beginner with complicated SQL queries thus the limitation to translate the following pseudocode into reality:
SELECT field_1, field_2, field_3
CREATE new_field
// these would be WHERE-queries
if field_1 & field_2 are "missing", "unknown" or empty
then new_field = field_3
else if field_2 & field_3 are "missing", "unknown" or empty
then new_field = field_1
else if field_1 & field_3 are "missing", "unknown" or empty
then new_field = field_2
The new_field
will basically contain the values of combined field_1
, field_2
, field_3
... field_n
. Assume that only one of these n-fields have a legit value.
Is this achievable in MS Access using a simple query?
回答1:
If you will be running the query within an Access session you can use Nz
. This will return the value of the field which contains a non-Null value ...
SELECT Nz(field_1, '') & Nz(field_2, '') & Nz(field_3, '') AS combined
If you need a query which can work from outside an Access session, Nz
is not available. Use an IIf
expression instead.
SELECT
IIf(field_1 Is Null, '', field_1)
& IIf(field_2 Is Null, '', field_2)
& IIf(field_3 Is Null, '', field_3)
来源:https://stackoverflow.com/questions/19251590/combine-multiple-fields-with-condition