Skip to content
Advertisement

In SQL, how do I coalesce the first value of 3 columns that is 10 characters long

I am trying to only select one column from 3 distinct phone number columns, and I would prefer to have the column with the longest phone number (some don’t have area codes). Currently, I have coalesced the 3 columns as some are also null, with a preference on cell phone.

Ex.

What I’ve tried :

I would prefer to just have the first number that is 10 characters long or the longest of the 3 columns, I can only have one column returned. Bonus points if it can keep a preference of cell_number , but not required.

@@version : Microsoft SQL Server 2019 (RTM) – 15.0.2000.5 (X64) Sep 24 2019 13:48:23 Copyright (C) 2019 Microsoft Corporation Express Edition (64-bit) on Windows 10 Enterprise 10.0 (Build 18363: ) (Hypervisor)

Advertisement

Answer

You can use a case expression. However, outer apply is simpler than a big case expression — and generalizes to more phone numbers much more easily:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement