我正在使用sql中的表来使用现有的电话号码列添加新列,并在新列中过滤有效和无效的电话号码。
这是条件 -
1.电话号码应为10位数,应以8或9开头
2.电话号码应为11位,前两位应为0和9
3.电话号码应为12位,前两位应为9和1
4.电话号码不应以任何字母开头。
5.如果电话号码遵守上述任何一个条件,则它应显示在新列中,否则应显示为无效或无效。
这是我尝试但不工作的东西:
Select *, ROW_NUMBER() over (partition by [Phone No] order by [Customer No] DESC) as RowNumber,
(Select(ltrim(rtrim([Phone No])) like '[^a-z]%' and
((len(ltrim(rtrim([Phone No]))) = 10 and [Phone No] like '[89]%') or ([Phone No] like
case when (len(ltrim(rtrim([Phone No]))) = 11 and [Phone No] like '[0][9]%') then
[Phone No]
end) or (len(ltrim(rtrim([Phone No]))) = 12 and [Phone No] like '[9][1]%'))) from
[Data_Joined])
from [Data_Joined]
请帮忙!感谢。
答案 0 :(得分:0)
我不能说它是有效的答案,但仍能满足您的要求。
select case when flg= 'V' then phone_no else null end as phone_no
from
(select phone_no ,'V' AS flg
from Data_Joined
where ltrim(rtrim(phone_no)) like '[^a-z]%'
and (phone_no like
case when len(ltrim(rtrim(phone_no))) = 12 and phone_no like '[9][1]%' then
phone_no end )
or (len(ltrim(rtrim(phone_no))) = 10 and phone_no like '[89]%') or (phone_no like
case when len(ltrim(rtrim(phone_no))) = 11 and phone_no like '[0][9]%' then
phone_no
end )
union
select phone_no,'I' AS flg from Data_Joined WHERE phone_no NOT in (select phone_no
from Data_Joined
where ltrim(rtrim(phone_no)) like '[^a-z]%'
and (phone_no like
case when len(ltrim(rtrim(phone_no))) = 12 and phone_no like '[9][1]%' then
phone_no end )
or (len(ltrim(rtrim(phone_no))) = 10 and phone_no like '[89]%') or (phone_no like
case when len(ltrim(rtrim(phone_no))) = 11 and phone_no like '[0][9]%' then
phone_no
end))) a
<强>输出:强>
phone_no
NULL
9029646510
919069745510
09039478311
NULL
答案 1 :(得分:0)
感谢您的帮助! 这是简化版本:
Insert into [Data_Joined_1]
Select *, case
when (len(ltrim(rtrim([phone_no]))) = 10 and
ltrim(rtrim([phone_no])) like '[89]%' and [phone_no] not like '%[a-
z]%') then [phone_no]
when (len(ltrim(rtrim([phone_no]))) = 11 and
ltrim(rtrim([phone_no])) like '[0][89]%' and [phone_no] not like '%[a-
z]%') then right([phone_no],10)
when (len(ltrim(rtrim([phone_no]))) = 12 and
ltrim(rtrim([phone_no])) like '[9][1]%' and [phone_no] not like '%
[a-z]%') then right([phone_no],10)
end as Filtered
from [Data_Joined]