Combine multiple fields with condition

点点圈 提交于 2020-01-25 05:35:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!