Casting char(3) to integer

Analytics
Fan

Casting char(3) to integer

Good Day Forum,

I'm new to TD.  this is my first post here.

I have a scenario where i need to convert a char(3) to integer

Eg:

Value

-13 --- characters length should be 3  but i get 13 space characters so i cast(substr( MaxTemp,1,3) as char(3)). now i need to convvert the char(3) to integer

My script:

cast(cast(substr( MaxTemp,1,3) as char(3)) as integer) but i get an errror 

Failed 2621 bad character in format or data of table

Thanks in advance

1 REPLY
Junior Supporter

Re: Casting char(3) to integer

No need to do that:

 BTEQ -- Enter your SQL request or BTEQ command:

CREATE TABLE MY_DB.PRUEBA01(ID_N INTEGER NOT NULL,

                            C_TXT CHAR(13) NOT NULL)

                            UNIQUE PRIMARY INDEX(ID_N)

;

 *** Table has been created.

 *** Total elapsed time was 1 second.

 BTEQ -- Enter your SQL request or BTEQ command:

INSERT INTO MY_DB.PRUEBA01 (ID_N, C_TXT) VALUES (-13,'-13');

 *** Insert completed. One row added.

 *** Total elapsed time was 1 second.

 BTEQ -- Enter your SQL request or BTEQ command:

SELECT ID_N, CAST(C_TXT AS INTEGER) FROM MY_DB.PRUEBA01;

 *** Query completed. One row found. 2 columns returned.

 *** Total elapsed time was 1 second.

       ID_N        C_TXT

-----------  -----------

        -13          -13

HTH.

Cheers.

Carlos.