Conversion from VARCHAR to INT

Asked

Viewed 14,068 times

2

I’m trying to do INNER JOIN but the fields are in VARCHAR.

In a field I have cod_authorization = 749831164

And in the other field I have num_aut = 000749831164

Because of the zeroes "Left" connection does not work.

I thought I’d convert them to INT but it’s not working, I believe it is some syntax error.

SELECT 

controle.num_aut,estoque.cod_autorizacao 

FROM cad_controle AS controle
INNER JOIN estoque AS estoque WITH(NOLOCK) ON CAST(estoque.cod_autorizacao AS INT)=CAST(controle.num_aut AS INT)

Error that appears:

The conversion of the varchar value '9977613700' overflowed an int column.
  • The ideal would be to compare everything as varchar same, num_aut is of fixed size? for example always has 10 digits, 12345 flipped 0000012345

  • 2

    Maybe bigint. The error message says that the number is too big for the int type.

  • @rray the 2 fields are varchar(50)

  • @bfavaretto tried to convert to bigint and This error appeared: Error Converting data type varchar to bigint

  • 1

    @Shaolymphantastic This error must be appearing probably because the column has some value that is not numerical.

1 answer

6


The error is happening because you are converting a value that does not fit the type INT, then ends up giving overflow in conversion. No SQL Server, there are 4 types of variables to treat integers, they are they: TINYINT, SMALLINT, INT and BIGINT.

Below follows the maximum values supported by these types:

  • TINYINT: 0 to 255.
  • SMALLINT: -32,768 to 32,767.
  • INT: -2,147,483,648 to 2,147,483,647
  • BIGINT: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

So, to solve your problem, try to run the QUERY thus:

    SELECT
           controle.num_aut ,
           estoque.cod_autorizacao 
      FROM cad_controle AS controle
INNER JOIN estoque AS estoque WITH ( NOLOCK )
        ON CONVERT ( BIGINT , estoque.cod_autorizacao ) = CONVERT ( BIGINT , controle.num_aut )

Editing:

When executing the query, if you come across this error message: "Error converting data type varchar to bigin", then there is probably a value não numérico in any of the columns, to solve this case, try to run the query below, to fix this problem, only that way, talvez your INNER JOIN doesn’t work the way you want it to.

    SELECT
           controle.num_aut ,
           estoque.cod_autorizacao 
      FROM cad_controle AS controle
INNER JOIN estoque AS estoque WITH ( NOLOCK )
        ON 
           ( CASE WHEN ( ISNUMERIC ( estoque.cod_autorizacao ) = 1 ) THEN CONVERT ( BIGINT , estoque.cod_autorizacao ) ELSE 0 END ) =
           ( CASE WHEN ( ISNUMERIC ( controle.num_aut ) = 1 ) THEN CONVERT ( BIGINT , controle.num_aut ) ELSE 0 END )
  • 1

    This error appeared: Error Converting data type varchar to bigint.

  • @Shaolinfantastic I gave an edited answer, so that others can better understand why their error was occurring, and I also modified the query, try with that new edition query.

  • @Shaolinfantastic posted a new QUERY that might solve your problem with the conversion of VARCHAR.

  • Cara really liked your solution but I’m starting to think it’s some version problem because it keeps bringing Error:Error Converting data type varchar to bigint. I use SQL SERVER 2016

  • @Shaolinfantastic I am testing on a Microsoft SQL Server 2014 - 12.0.2000.8 (Intel X86) and the second query (using the CASE) it works, I find it very difficult to have changed anything in relation to that.

  • It worked! I had not put the "CASE" Thank you very much!

  • 1

    Oh that great @Shaolinfantastic! Just do some tests and make sure there will be no problems with your INNER JOIN due to the CASE return 0.

  • 1

    With the following query, you find out which lines have problems: SELECT controle.num_aut , estoque.cod_autorizacao FROM cad_controle AS controle WHERE ISNUMERIC ( estoque.cod_autorizacao ) = 0 OR ISNUMERIC ( estoque.cod_autorizacao ) = 0

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.