我正在尝试在查询中构建一个不同的字符串,除非其中一个值为NULL,否则它将起作用。我已经测试过删除了LOCATION_ADDR_LINE_2,查询就可以了。当我没有SELECT DISTINCT时,我发现LOCATION_ADDR_LINE_2值为NULL。即使NULL?
,如何在SELECT DISTINCT中收集这些值?SELECT DISTINCT(LOCATION_ADDR_LINE_1 + ', ' + LOCATION_ADDR_LINE_2 + ', ' + LOCATION_CITY + ', ' + LOCATION_WORK_STATE) AS Addresses
FROM OracleReport
WHERE (LOCATION_ADDR_LINE_1 LIKE '%1135 Auto%' OR LOCATION_ADDR_LINE_1 LIKE '%199 Easy%')
返回:
地址 NULL
SELECT DISTINCT(LOCATION_ADDR_LINE_1 + ', ' + LOCATION_CITY + ', ' + LOCATION_WORK_STATE) AS Addresses
FROM [OperationReport].[dbo].[OracleReport]
WHERE (LOCATION_ADDR_LINE_1 LIKE '%1135 Auto%' OR LOCATION_ADDR_LINE_1 LIKE '%199 Easy%')
返回:
地址
1135自动...
189-199容易......
答案 0 :(得分:1)
当值为NULL时,假设您不介意text,,text,...
(空字符串)......
SELECT DISTINCT(coalesce(LOCATION_ADDR_LINE_1,'') + ', ' +
coalesce(LOCATION_ADDR_LINE_2,'') + ', ' +
coalesce(LOCATION_CITY,'') + ', ' +
coalesce(LOCATION_WORK_STATE,'')) AS Addresses
FROM OracleReport
WHERE (LOCATION_ADDR_LINE_1 LIKE '%1135 Auto%'
OR LOCATION_ADDR_LINE_1 LIKE '%199 Easy%')
Coalesce将获取第一个非null值并返回它。它需要一致的数据类型,并且一旦遇到系列中的第一个非空值,就会提前退出。 (更多详情Oracle Differences between NVL and Coalesce)