Home Page for the TeradataForum

Archives of the TeradataForum

Message Posted: Thu, 06 Mar 2003 @ 14:19:42 GMT

  <Prev Next>   <<First <Prev Next> Last>>  

Subj:   Re: SQL: integer part of a float_field
From:   Geoffrey Rommel

  How can I retrieve the integer part of a float_field?  

The short answer is: float_field - (float_field MOD 1)

Here's the long answer. The behavior of division operators (/ and MOD) depends on the data type of the field, as explained in the SQL Reference, thus:

int_field / int_field TRUNCATES and returns an integer
dec_field / dec_field ROUNDS and returns a decimal
float_field / float_field rounds and returns a float

If there were such a thing as a "truncating division" operator, you could select float_field TDIV 1; this would divide by 1 and discard the remainder, leaving you with the integer part (e.g., 9.876 TDIV 1 would be 9). Unfortunately, there is no such operator; truncating division is available only with integers. Thus, you must select the expression shown above. Note, however, that the calculation requires a floating-point division and a subtraction, so it's possible the answer will be slightly imprecise (e.g. 9.00000000002), and the answer will be of type FLOAT. To change that, you must cast it to something else:

float_field - (float_field MOD 1) (integer) if you're sure it will never be greater than 2^31, or float_field - (float_field MOD 1) (decimal(16,0))

  <Prev Next>   <<First <Prev Next> Last>>  
  Top Home Privacy Feedback  
Copyright for the TeradataForum (TDATA-L), Manta BlueSky    
Copyright 2016 - All Rights Reserved    
Last Modified: 28 Jun 2020