30

I have a field that is VARCHAR(6) I am trying to insert it into another table of type bigint it is giving me an error

(Error Converting from data type varchar to bigint

here is what i am doing

CONVERT(bigint, seconds) as seconds

Can anybody help with this issue?

5
  • Have you checked the data to make sure that the values in the field you're converting are actually convertible to int?
    – Paul Sasik
    Jan 31, 2013 at 22:28
  • seems that one of your varchar is not convertible... Jan 31, 2013 at 22:28
  • I have it either have a null, -number and positive number! but i did isnull(second, 0) and i still have the same issue Convert(bigint, ISNULL(ts.TimeInSeconds,0) ) AS seconds
    – Jaylen
    Jan 31, 2013 at 22:35
  • 2
    try to find it with a where isnumeric(seconds) = 0
    – dotjoe
    Jan 31, 2013 at 22:38
  • isnumeric(seconds) = 0 did not help but gave me an idea and it worked. Convert(bigint, isnumiric(ts.TimeInSeconds) ) AS second Thanks a lot for your help
    – Jaylen
    Feb 1, 2013 at 0:07

4 Answers 4

47

This is the answer

(CASE
  WHEN
    (isnumeric(ts.TimeInSeconds) = 1) 
  THEN
    CAST(ts.TimeInSeconds AS bigint)
  ELSE
    0
  END) AS seconds
3
  • 3
    Might want to edit this to be correct, as isnumiric won't exist (check spelling). Also, even though CONVERT() (and ISNUMERIC()) works on your machine, you've tagged your question as 'sql', which is a language - your machine has a particular implementation (SQL Server). The standard is CAST(... as BIGINT) - for future viewers, it may be best to either tag your question with the relevant RDBMS, or write a 'standards' version. Feb 1, 2013 at 1:02
  • So what you wanted was to insert a 1 if the value in TimeInSeconds is a valid integer and 0 if it is not a valid integer? Don't you want the value in column TimeInSeconds inserted somewhere? Feb 1, 2013 at 6:22
  • I have corrected my answer after figuring out the problem. Thanks again for your help guys :)
    – Jaylen
    Feb 1, 2013 at 17:40
8

an alternative would be to do something like:

SELECT
   CAST(P0.seconds as bigint) as seconds
FROM
   (
   SELECT
      seconds
   FROM
      TableName
   WHERE
      ISNUMERIC(seconds) = 1
   ) P0
3

I think your code is right. If you run the following code it converts the string '60' which is treated as varchar and it returns integer 60, if there is integer containing string in second it works.

select CONVERT(bigint,'60') as seconds 

and it returns

60
-1

A wrinkle on this problem occurs when the text 'number' has a decimal point, at least in TSQL, that causes the solution above to not work.

Just posting in case it might help others. Drove me crazy for an hour. A solution seems to be to first converting to float and then to bigint.

select cast(1234567891011.0 as varchar(50)) as numb1 into #t1; select * from #t1;

select numb1 ,cast(numb1 as bigint) as numb1x from #t1; --Error converting data type varchar to bigint.

--Even though it is a number: select numb1 ,isnumeric(numb1) as isnumb from #t1;

--proposed solution above fails: select numb1, CASE WHEN isnumeric(numb1)=1 then cast(numb1 as bigint) ELSE 0 END as numb1x from #t1; --Error converting data type varchar to bigint.

--works: select numb1 ,cast(cast(numb1 as float) as bigint) as numb1x from #t1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.