December 15, 2014 at 3:59 am
Hi All,
I have a table with phone number column, it contains land phone numbers & mobile numbers as well.
1. A phone number without code will be 7 in length
2. A ph number with length 10,11
3. mobile number with length 10,11
Say my phone number code 088 2345678 and mobile number is 2345678919 or 2345678919
data is available in below formats:
1.mob2345678house
2.2345678da
3.2345xx678
4.2345678919this is my number
5.office2345678919
6.2345xhk678919
7.2345678919callme
I want to remove all the alphabets before number starts and after the number and if the number has any alphabets in between the number they should go as invalid records.
Can any body suggest ,how can we achieve With out using any CLRs and using only SQL .
Thanks
December 15, 2014 at 4:53 am
December 15, 2014 at 6:01 am
This supports only 4 rules you have mentioned. No '()', '-' or whitespaces allowed.
declare @s-2 varchar(50) = 'fd123456asa789013dfd';
select @s-2
, f.Flag
, nmbr = case f.Flag when 'OK' then substring(@s,f.strt,f.ln) end
from (select null dummy ) dummy
cross apply (
select cd1 = nullif(patindex('%[^0-9][0-9]%',@s),0)
, cde = len(@s) - nullif(patindex('%[0-9][^0-9]%',reverse(@s)),0)
, dc1 = nullif(patindex('%[0-9][^0-9]%',@s),0)
, dce = len(@s) - nullif(patindex('%[^0-9][0-9]%',reverse(@s)),0) ) as pos
cross apply (
select flag = case when @s-2 is null
or patindex('%[0-9]%',@s) = 0
or isnull(pos.dc1,len(@s)) < isnull(pos.cde,0) then 'Bad number'
when isnull(dce,len(@s))-isnull(cd1,0) not in (7,10,11) then 'Bad length'
else 'OK' end
,strt = isnull(cd1,0)+1
,ln = isnull(dce,len(@s))-isnull(cd1,0)
) as f
Hope it helps.
February 26, 2021 at 2:21 pm
How i can use a column name instead of providing number, i have phone numbers provided in column "TELEPHONE" on table "ADDRESS", so how i can use below query to validate whole column in once.
this is great and i was looking for this solution, please help
declare @s-2 varchar(50) = 'fd123456asa789013dfd';
select @s-2
, f.Flag
, nmbr = case f.Flag when 'OK' then substring(@s,f.strt,f.ln) end
from (select null dummy ) dummy
cross apply (
select cd1 = nullif(patindex('%[^0-9][0-9]%',@s),0)
, cde = len(@s) - nullif(patindex('%[0-9][^0-9]%',reverse(@s)),0)
, dc1 = nullif(patindex('%[0-9][^0-9]%',@s),0)
, dce = len(@s) - nullif(patindex('%[^0-9][0-9]%',reverse(@s)),0) ) as pos
cross apply (
select flag = case when @s-2 is null
or patindex('%[0-9]%',@s) = 0
or isnull(pos.dc1,len(@s)) < isnull(pos.cde,0) then 'Bad number'
when isnull(dce,len(@s))-isnull(cd1,0) not in (7,10,11) then 'Bad length'
else 'OK' end
,strt = isnull(cd1,0)+1
,ln = isnull(dce,len(@s))-isnull(cd1,0)
) as f
February 26, 2021 at 4:20 pm
SELECT
phone_string,
phone,
--LEN(phone) AS phone_len,
CASE WHEN
CASE WHEN phone LIKE '%[^0-9]%' THEN 0
WHEN LEN(phone) IN (7, 10, 11) THEN 1 ELSE 0 END
= 0 THEN 'Invalid' ELSE 'Valid' END AS phone_status
FROM ( VALUES
(1, 'mob2345678house'), (2, '2345678da'), (3, '2345xx678'),
(4, '2345678919this is my number'), (5, 'office2345678919'),
(6, '2345xhk678919'), (7, '2345678919callme'),
(8, 'bunchoflettersnonumber') --<<--added row with no digits at all
) AS data(id, phone_string)
CROSS APPLY (
SELECT PATINDEX('%[0-9]%', phone_string) AS phone_first_digit,
LEN(phone_string) - PATINDEX('%[0-9]%', REVERSE(phone_string)) + 1 AS phone_last_digit
) AS ca1
CROSS APPLY (
SELECT CASE WHEN phone_first_digit = 0 THEN '' ELSE SUBSTRING(phone_string,
phone_first_digit, phone_last_digit - phone_first_digit + 1) END AS phone
) AS ca2
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply