Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, im have some data from SQL server and i need to convert bigint to int. any idea how?

Replies are listed 'Best First'.
Re: convert bigint to int
by syphilis (Archbishop) on Apr 05, 2017 at 14:38 UTC
    This script creates a variable that's a Math::BigInt object, and then changes that variable into a normal integer value:
    use warnings; use strict; use Math::BigInt; use Devel::Peek; my $x = Math::BigInt->new(42); Dump $x; # Shows that $x is a # Math::BigInt object print "\n ########\n ########\n\n"; $x = "$x" + 0; Dump $x; # Shows that $x is # a normal integer
    That's my answer to the question that I'm guessing you want answered.

    That script outputs:
    SV = PV(0x2ee308) at 0x3481a0 REFCNT = 1 FLAGS = (ROK) RV = 0x2ed750 SV = PVHV(0x32d528) at 0x2ed750 REFCNT = 1 FLAGS = (OBJECT,SHAREKEYS) STASH = 0x350118 "Math::BigInt ARRAY = 0x334b38 (0:6, 1:2) hash quality = 125.0% KEYS = 2 FILL = 2 MAX = 7 Elt "sign" HASH = 0x46c14520 SV = PV(0x2ee298) at 0x33dd58 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x24f9288 "+"\0 CUR = 1 LEN = 10 COW_REFCNT = 1 Elt "value" HASH = 0xaea09fe1 SV = IV(0x33de38) at 0x33de48 REFCNT = 1 FLAGS = (ROK) RV = 0x33dda0 SV = PVAV(0x2ef240) at 0x33dda0 REFCNT = 1 FLAGS = () ARRAY = 0x2871cc8 FILL = 0 MAX = 0 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x33dd78) at 0x33dd88 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 42 PV = 0x2ed750 "" CUR = 0 LEN = 0 ######## ######## SV = PVIV(0x346738) at 0x3481a0 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 42 PV = 0
    Only caveat I can spot is that if $x had been created as my $x = 42; then the PV slot would not have been set at all (on perl-5.24.0, anyway).

    Cheers,
    Rob
Re: convert bigint to int
by huck (Prior) on Apr 05, 2017 at 08:18 UTC

    What code do you use to access and get the data from the sql server.

    What sql server is it?

      my $SQL= "SELECT TOP 1000 [name_software] ,[first_term] ,[term_sofi] ,[max_term] FROM [ADM_SHARED].[dbo].[mida_rights]"; my $Select = $dbh->prepare($SQL); $Select->execute(); while(my $Row=$Select->fetchrow_hashref) { print "$Row->{name_software}\t $Row->{first_term}\t $Row->{term_sofi +}\t"; print "\n"; } $dbh->disconnect;
      $Row->{first_term} is in bigint and i want it to be int (sql server 2008).

        What does your $dbh->connect look like. We need to know what sort of driver you are using

        What error do you get back from what you are trying.

        Are you 64bit perl on a 64bit machine?

Re: convert bigint to int
by thanos1983 (Parson) on Apr 05, 2017 at 09:34 UTC

    Hello Anonymous Monk,

    Take a look at (bigint). Is this something that you are trying to get?

    #!/usr/bin/perl use strict; use warnings; sub three_integer { use integer; return 3.2; } sub three_bigint { use bigint; return 3.2; } print three_integer(), " ", three_bigint(),"\n"; # prints "3.2 3" __END__ $ perl test.pl 3.2 3

    Although I do not understand the reason that you are doing this as huck says but this could be a solution to your problem.

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: convert bigint to int
by chacham (Prior) on Apr 05, 2017 at 18:24 UTC

    The best way to explicitly convert from one data type to another in SQL Server is with CAST(). So, assuming the data fit inside an int, CAST(column-name AS INT). If you are not sure if it will fit, TRY_CAST() might be what you are looking for.