SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select only numeric content
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ranalk
Starting Member

48 Posts

Posted - 06/06/2012 :  03:53:40  Show Profile  Reply with Quote
Hi,

I have a column that has phone numbers.
There is no validation on insert and some have leading + and some have characters.
What is the efficient way to extract these records the once that are not pure phone number.

Thanks!

webfred
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 06/06/2012 :  04:07:47  Show Profile  Visit webfred's Homepage  Reply with Quote
To get the rows with non numeric chars you can go like this:
select * from table where phone like '%[^0-9]%'


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

ranalk
Starting Member

48 Posts

Posted - 06/06/2012 :  04:40:05  Show Profile  Reply with Quote
quote:
Originally posted by webfred

To get the rows with non numeric chars you can go like this:
select * from table where phone like '%[^0-9]%'


No, you're never too old to Yak'n'Roll if you're too young to die.



Thanks.

Is there any easy way to substring the prefixes that are not numeric.
Substring should be from the beginning till the first number.
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 06/06/2012 :  06:55:25  Show Profile  Visit webfred's Homepage  Reply with Quote
If that isn't what you want then please give example data:

select
left(Phone,(patindex('%[0-9]%',Phone))-1),
*
from table
where Phone like '%[^0-9]%'


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

raghuveer125
Constraint Violating Yak Guru

India
277 Posts

Posted - 06/06/2012 :  10:30:55  Show Profile  Reply with Quote
Hi Webfred Nice logic
this is working for first case
Declare @a Table (phone nvarchar(20))
Insert into @a values('9906291558')
Insert into @a values('99062a91558')
Insert into @a values('86506v25854')
--Select * from @a Where phone like ('%[^0-9]%')
select
left(Phone,(patindex('%[^0-9]%',Phone))-1)+
Right(Phone,(patindex('%[^0-9]%',Phone))-1),*
from @a
where Phone like '%[^0-9]%'


--What about this case
Declare @b Table (phone nvarchar(20))
Insert into @b values('9906291558')
Insert into @b values('99062a91558')
Insert into @b values('865062v5854')
select
left(Phone,(patindex('%[^0-9]%',Phone))-1)+
Right(Phone,(patindex('%[^0-9]%',Phone))-1),*
from @b
where Phone like '%[^0-9]%'

--And in This case
Declare @c Table (phone nvarchar(20))
Insert into @c values('9906291558')
Insert into @c values('99062abc91558')
Insert into @c values('86a5062v5854')
select
left(Phone,(patindex('%[^0-9]%',Phone))-1)+
Right(Phone,(patindex('%[^0-9]%',Phone))-1),*
from @c
where Phone like '%[^0-9]%'

--And in this case
Declare @d Table (phone nvarchar(20))
Insert into @d values('+9906291558')
Insert into @d values('99062abc91558')
Insert into @d values('+86a5062v5854')
select
left(Phone,(patindex('%[^0-9]%',Phone))-1)+
Right(Phone,(patindex('%[^0-9]%',Phone))-1),*
from @d
where Phone like '%[^0-9]%'


In Love... With Me!
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 06/06/2012 :  11:05:53  Show Profile  Visit webfred's Homepage  Reply with Quote
If the question is: get only the numeric part from a string then you can learn so much from madhivanan:
http://beyondrelational.com/modules/2/blogs/70/posts/10821/extract-only-numbers-from-a-string.aspx


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 06/06/2012 :  11:08:54  Show Profile  Visit webfred's Homepage  Reply with Quote
and this:
http://beyondrelational.com/modules/2/blogs/70/posts/10894/extracting-numbers-part-2.aspx

and this:
http://beyondrelational.com/modules/2/blogs/70/posts/10946/extract-only-numbers-from-a-string-part-3.aspx


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Sachin.Nand
Flowing Fount of Yak Knowledge

2937 Posts

Posted - 06/08/2012 :  02:19:52  Show Profile  Reply with Quote
quote:
Originally posted by webfred

To get the rows with non numeric chars you can go like this:
select * from table where phone like '%[^0-9]%'


No, you're never too old to Yak'n'Roll if you're too young to die.



Now that my development knowledge is getting a bit rusted ,would the above query same as

select * from table where phone not like '%[0-9]%'

After Monday and Tuesday even the calendar says W T F ....
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 06/08/2012 :  02:41:17  Show Profile  Visit webfred's Homepage  Reply with Quote
quote:
Originally posted by Sachin.Nand

quote:
Originally posted by webfred

To get the rows with non numeric chars you can go like this:
select * from table where phone like '%[^0-9]%'


No, you're never too old to Yak'n'Roll if you're too young to die.



Now that my development knowledge is getting a bit rusted ,would the above query same as

select * from table where phone not like '%[0-9]%'

After Monday and Tuesday even the calendar says W T F ....


No - it is absolut different.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Sachin.Nand
Flowing Fount of Yak Knowledge

2937 Posts

Posted - 06/08/2012 :  02:48:25  Show Profile  Reply with Quote
Oh sorry I meant

select * from table where phone not like '%[a-z]%'

After Monday and Tuesday even the calendar says W T F ....
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 06/08/2012 :  02:54:52  Show Profile  Visit webfred's Homepage  Reply with Quote
quote:
Originally posted by Sachin.Nand

Oh sorry I meant

select * from table where phone not like '%[a-z]%'

After Monday and Tuesday even the calendar says W T F ....


To get only rows with non numeric chars the word "not" is wrong there.
But it is still different because [^0-9] captures more than only a-z.


No, you're never too old to Yak'n'Roll if you're too young to die.

Edited by - webfred on 06/08/2012 02:57:08
Go to Top of Page

vinu.vijayan
Posting Yak Master

India
227 Posts

Posted - 06/14/2012 :  06:37:40  Show Profile  Reply with Quote
You can use the following query to exclude '+' and only Select the PhoneNo.:


Select Ltrim(PARSENAME(Replace(Column_Name, '+', '.'), 1))


N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000