Set default value in query when value is null

前端 未结 6 1334
难免孤独
难免孤独 2021-02-04 08:16

I\'m running a really simple query, however for some of the results the value in one field is null. How can I set that value to \"a string\" if its value is null?

Someth

6条回答
  •  情歌与酒
    2021-02-04 08:51

    To avoid some problems to other users :

    Problem 1 + Solution :

    If you are planning to use the ISNULL function with an Access Database, be aware of the fact that the ISNULL function implemented in Access is differrent from the SQL Server implementation.

    Access ISNULL function always returns TRUE or FALSE. So you have to use a custom IIF function around your ISNULLusage.

    Problem 2 + Solution :

    Moreover, if you wish to use the same field name as Alias for your column, this could lead the Access Database Engine to inform you that there is a "circular reference" to the field.

    So if you need to use the same field name as Alias, you just have to add the table name before the field name. (example : You have to use RegTakePart.Website instead of simply Website). This way, you can freely use Website as Alias name for your column.

    The global working (and tested) SQL query to avoid these 2 problems is the following one :

    SELECT RegName,
        RegEmail,
        RegPhone,
        RegOrg,
        RegCountry,
        DateReg,
        IIF(RegTakePart.Website IS NULL, 'no website, RegTakePart.Website) AS Website
    FROM RegTakePart
    WHERE Reject IS NULL
    

提交回复
热议问题