?
Solved

Trying to do a conversion to BIGINT, invalid data in input - TSQL - SQL Server

Posted on 2012-08-31
5
Medium Priority
?
572 Views
Last Modified: 2012-08-31
I have a column on a table this is a phone number.  There must be bad data in one record.  It is causing a problem when trying to convert.

Is there a way I can test the column before I convert. The statement works for all other runs except for this one where there must be bad data somewhere.

 (SELECT top 1 cast(member_phone.member_phone as BIGINT)
  FROM TableA.member_phone member_phone
  WHERE member_phone.member_id = m.member_id AND    member_phone.primary_phone='Y')
0
Comment
Question by:morinia
5 Comments
 
LVL 13

Accepted Solution

by:
LIONKING earned 2000 total points
ID: 38354154
You could probably validate that the member_phone field contains indeed a numeric value.

Maybe this will help:

SELECT top 1 cast(member_phone.member_phone as BIGINT)
  FROM TableA.member_phone member_phone
  WHERE member_phone.member_id = m.member_id AND    member_phone.primary_phone='Y' AND ISNUMERIC(member_phone.member_phone)=1
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38354158
SELECT *
FROM TableA.member_phone
WHERE NOT ISNUMERIC(member_phone)>0
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38354169
ISNUMERIC(your_char_value) will test that it's at least a number.

Question for you:  What's the purpose of CASTing a phone number?  Most process do not perform any math on it, and it's just displayed like any other text field, so most people will treat it as text.
0
 

Author Closing Comment

by:morinia
ID: 38354311
I was converting to BIGINT so when the output went to an Excel Spreadsheet I could custom format to 999-999-9999
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 38354362
How are you storing your phone number?  isNumeric is a good start, but is very liberal with what it considers a number, and may well not find your problem.  

As examples, isNumeric will consider '609', '6.09', '$609', '-609', '+609', '¢609', '£609', '¤609', '¥609' as numbers (because the currency signs are acceptable),  but '6.0.9', '6 09', '#6 09', '6 09%', '=609', '', and ' ' all not-numbers. It also accepts TAB, CR, and LF characters in front of numbers, so if those are in your strings you may not find them. Finally, it'll accept things like '1E2' and '1D2'.

Basically, isNumeric() is really isNumericOfAnySortOrMoney().


As an alternative, if you're looking for pretty much anything but digits, try this:

NOT LIKE '%[^0-9]%'

It'll be slower than isNumeric() if you just apply it to the whole table, but it should pull out pretty much anything bad in there.

hth

Mike
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question