[sql] SQL, How to convert VARCHAR to bigint?

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?

This question is related to sql sql-server

The answer is


This is the answer

(CASE
  WHEN
    (isnumeric(ts.TimeInSeconds) = 1) 
  THEN
    CAST(ts.TimeInSeconds AS bigint)
  ELSE
    0
  END) AS seconds

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

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