简体   繁体   中英

How to get records where at least one of the fields has a number in SQL Server 2014?

I have a table that has 3 phone number fields (among other fields).

  1. DayTimePhone
  2. EveningPhone
  3. MobilePhone

I need to pull up records where there is a number in at least one of them.

SELECT Field1
      ,Field2
      ,Field3
      ,DayTimePhone
      ,EveningPhone
      ,MobilePhone
FROM Contact

WHERE DayTimePhone IS NOT NULL
  AND EveningPhone IS NOT NULL
  AND MobilePhone IS NOT NULL

But in some results I get rows where all three fields are blank. They don't have a NULL, just blank. So my code works to an extent.

I tried:

  SELECT Field1, Field2, Field3
  ,CASE isnull (DayTimePhone, '-') WHEN '' THEN '-' WHEN ' ' THEN'-'
   END DayTimePhone
  ,CASE isnull (EveningPhone, '-') WHEN '' THEN '-' WHEN ' ' THEN'-'
   END EveningPhone
  ,CASE isnull (MobilePhone, '-') WHEN '' THEN '-' WHEN ' ' THEN'-'
   END MobilePhone

  INTO #TempTable
--------------------------------
SELECT * FROM #TempTable
WHERE DayTimePhone IS NOT NULL
   AND EveningPhone IS NOT NULL
   AND MobilePhone IS NOT NULL
   AND DayTimePhone != '-'
   AND EveningPhone != '-'
   AND MobilePhone != '-'

But this code only gives me results WHERE there is a NULL or - in the fields.

How do I get results where there is at least 1 number in any of the three fields?

Edit

Please see my comment to @LONG answer. That solution worked but now the results are also showing rows where the DayTimePhone reads 0. Please read my comment below.

Another option is to simply check the length of the string representing the number. If it is null or too short then simply exclude it from the result.

SELECT * FROM #TempTable
WHERE   (
            (ISNULL(LEN(RTRIM([DayTimePhone])), 0) > 8)   
                OR 
            (ISNULL(LEN(RTRIM([EveningPhone])), 0) > 8) 
                OR 
            (ISNULL(LEN(RTRIM([MobilePhone])), 0) > 8) 
        )

Try:

SELECT * FROM #TempTable
WHERE (DayTimePhone IS NOT NULL AND REPLACE(DayTimePhone,' ','') != '')
   OR (EveningPhone IS NOT NULL AND REPLACE(EveningPhone ,' ','') != '')
   OR (MobilePhone IS NOT NULL AND REPLACE(MobilePhone ,' ','') != '')

Having blank is not always a good table design, try to update them with NULL

UPDATE: From you comments, could you please indicate which type 0 phone that is?

If you still see '0' in the result, that means that is definitely not a single 0 .

You can just go with WHERE DayTimePhone != '0-0-0' or DayTimePhone != '0-0' , but this is not a skeleton key . If you are checking with US phone numbers, you can go with Validate US Phone number and using LIKE , '[0-9]', expression pattern like this to check. I will try to give you a general way to check a validate US phone query.

A pretty simple way is:

SELECT tt.*
FROM #TempTable tt
WHERE tt.DayTimePhone <> '-' OR
      tt.EveningPhone <> '-' OR
      tt.MobilePhone <> '-';

NULL values will not return true for the <> comparison.

You should first clean you table from nulls and blank values. That was actually done already. What you need now is to use the OR command instead of AND and check it for the value you replaced ('-'):

SELECT * FROM #TempTable
WHERE (DayTimePhone IS NOT NULL != '-')
OR (EveningPhone IS NOT NULL  != '-')
OR (MobilePhone IS NOT NULL  != '-')

I hope it helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM