I'm trying to determine how to cleanup and populate a set of address lines in a SQL Server query. (Needs to be compatible with SQL Server 2008 and up)
I have 5 address lines Addr1 - Addr5 that I want to output into 4 consecutive populated columns in a query. If N addresses are non-null, then the final addresses FAddr1 thru FAddrN should be populated. (Any remaining columns should be null. If all 5 address lines are populated then the last one is dropped.)
Note that I might add a 6th Addr column later, but this doesn't have to be generically solved for any N columns. (6 Addr columns should be the max.) The data can't be standardized beforehand, so I have to do this on the fly in the fastest way possible.
The complexity is how to shift the address line columns "to the left" to overwrite the blank columns.
Here's an example:
Addr1 Addr2 Addr3 Addr4 Addr5
--------------------------------------------------------------------------------
101 Main St Apt 1 Here, AZ 11111 USA
102 Main St Here, AZ 22222
103 Main St Apt 3 Here, AZ 33333 USA
104 Main St Here, AZ 44444 USA
For these examples I would like to output:
FAddr1 FAddr2 FAddr3 FAddr4
----------------------------------------------------------------
101 Main St Apt 1 Here, AZ 11111
102 Main St Here, AZ 22222
103 Main St Apt 3 Here, AZ 33333 USA
104 Main St Here, AZ 44444 USA
I've written this in C#, but the complexity is doing this in SQL in a clean way. It's much easier (see related questions below) to do this by concatenating into one string. But I need these in 4 separate columns.
Related questions: Most Similar Question - Different Use Case / Similar But With String Instead of Columns
Initial Version:
I've written a version below in SQL using a test case that seems to be working, but I've hoping there is a cleaner way. This basically keeps shifting the non-null elements. But it takes a sub-select per column to shift everything to the left.
Select
Addr1,
case when Addr2 is null then Addr3 else Addr2 end as Addr2,
case when Addr2 is null then null else Addr3 end as Addr3,
case when Addr4 is null then Addr5 else Addr4 end as Addr4
-- Truncate to 4 - case when Addr4 is null then null else Addr5 end as Addr5
From
(
Select
case when Addr1 is null then Addr2 else Addr1 end as Addr1,
case when Addr1 is null then null else Addr2 end as Addr2,
case when Addr3 is null then Addr4 else Addr3 end as Addr3,
case when Addr3 is null then null else Addr4 end as Addr4,
Addr5
From
(
Select
Addr1,
case when Addr2 is null then Addr3 else Addr2 end as Addr2,
case when Addr2 is null then null else Addr3 end as Addr3,
case when Addr4 is null then Addr5 else Addr4 end as Addr4,
case when Addr4 is null then null else Addr5 end as Addr5
From
(
Select
case when Addr1 is null then Addr2 else Addr1 end as Addr1,
case when Addr1 is null then null else Addr2 end as Addr2,
case when Addr3 is null then Addr4 else Addr3 end as Addr3,
case when Addr3 is null then null else Addr4 end as Addr4,
Addr5
From
(
Select
Addr1,
case when Addr2 is null then Addr3 else Addr2 end as Addr2,
case when Addr2 is null then null else Addr3 end as Addr3,
case when Addr4 is null then Addr5 else Addr4 end as Addr4,
case when Addr4 is null then null else Addr5 end as Addr5
From
(
SELECT NULL as Addr1, NULL as Addr2, NULL as Addr3, NULL as Addr4, NULL as Addr5 UNION
SELECT NULL, NULL, NULL, NULL, '5' UNION
SELECT NULL, NULL, NULL, '4', NULL UNION
SELECT NULL, NULL, NULL, '4', '5' UNION
SELECT NULL, NULL, '3', NULL, NULL UNION
SELECT NULL, NULL, '3', NULL, '5' UNION
SELECT NULL, NULL, '3', '4', NULL UNION
SELECT NULL, NULL, '3', '4', '5' UNION
SELECT NULL, '2', NULL, NULL, NULL UNION
SELECT NULL, '2', NULL, NULL, '5' UNION
SELECT NULL, '2', NULL, '4', NULL UNION
SELECT NULL, '2', NULL, '4', '5' UNION
SELECT NULL, '2', '3', NULL, NULL UNION
SELECT NULL, '2', '3', NULL, '5' UNION
SELECT NULL, '2', '3', '4', NULL UNION
SELECT NULL, '2', '3', '4', '5' UNION
SELECT '1', NULL, NULL, NULL, NULL UNION
SELECT '1', NULL, NULL, NULL, '5' UNION
SELECT '1', NULL, NULL, '4', NULL UNION
SELECT '1', NULL, NULL, '4', '5' UNION
SELECT '1', NULL, '3', NULL, NULL UNION
SELECT '1', NULL, '3', NULL, '5' UNION
SELECT '1', NULL, '3', '4', NULL UNION
SELECT '1', NULL, '3', '4', '5' UNION
SELECT '1', '2', NULL, NULL, NULL UNION
SELECT '1', '2', NULL, NULL, '5' UNION
SELECT '1', '2', NULL, '4', NULL UNION
SELECT '1', '2', NULL, '4', '5' UNION
SELECT '1', '2', '3', NULL, NULL UNION
SELECT '1', '2', '3', NULL, '5' UNION
SELECT '1', '2', '3', '4', NULL UNION
SELECT '1', '2', '3', '4', '5'
) as Base
) as Pass1 ) as Pass2 ) as Pass3 ) as Pass4