DECLARE @Phone VARCHAR(50) = '(801) 123-4567 x9876';
SELECT @Phone =
RTRIM(LTRIM(REPLACE(REPLACE(REPLACE(REPLACE(
CASE
WHEN CHARINDEX('x', @Phone, 1) > 0 THEN SUBSTRING(@Phone, 1, CHARINDEX('x', @Phone, 1) - 1)
ELSE @Phone
END, '(', ''), ')', ''), ' ', ''), '-', '')))
SELECT @Phone
with mydata as (
select '555-555-5555' cnt1_cphone from dual
union all
select '1-555-555-5555' cnt1_cphone from dual
union all
select '(555)-555-5555' cnt1_cphone from dual
union all
select '555-555-5555 x55' cnt1_cphone from dual
)
select ':' || trim(regexp_replace(cnt1_cphone,'^1-|x.*$|[()]')) || ':' from mydata
/
:555-555-5555:
:555-555-5555:
:555-555-5555:
:555-555-5555:
What following the letter x?
What do you want changed in the case statement?