QuestionQ5
Design and develop database solutionsYou have a Microsoft SQL Server 2025 database containing a table named dbo.CustomerMessages. dbo.CustomerMessages has two columns: MessageID (int) and MessageRaw (nvarchar(max)).
MessageRaw can include a phone number in several formats, and some rows do not include a phone number.
Write one SELECT query that meets these requirements:
- Return
MessageID,RawNumber,DigitsOnly, andPhoneStatus. RawNumbermust contain the first substring that matches a phone-number pattern, orNULLwhen no match is found.DigitsOnlymust remove every non-digit character fromRawNumber, or returnNULL.PhoneStatusmust returnValidwhenMessageRawcontains a phone number; otherwise, it must returnMissing.
Each value may be used once, more than once, or not at all.
Drag & Drop
SELECT MessageID, MessageRaw, '\d{3}[\.\-\s]\d{3}[\.\-\s]\d{4}') AS RawNumber, MessageRaw, '\d{3}[\.\-\s]\d{3}[\.\-\s]\d{4}'), '\D', '') AS DigitsOnly, CASE WHEN MessageRaw, '\d{3}[\.\-\s]\d{3}[\.\-\s]\d{4}') = 1 THEN 'Valid' ELSE 'Missing' END AS PhoneStatus FROM dbo.CustomerMessages;
Community Discussion