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

Posted on 2012-08-31
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')
Question by:morinia
    LVL 13

    Accepted Solution

    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
    LVL 18

    Expert Comment

    SELECT *
    FROM TableA.member_phone
    WHERE NOT ISNUMERIC(member_phone)>0
    LVL 65

    Expert Comment

    by:Jim Horn
    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.

    Author Closing Comment

    I was converting to BIGINT so when the output went to an Excel Spreadsheet I could custom format to 999-999-9999
    LVL 16

    Expert Comment

    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.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    The steps for moving the system databases to a new location are documented in the following technical article: However sometimes after the moving process is finished, though SQL i…
    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
    Viewers will learn how the fundamental information of how to create a table.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now