11

Reviewing code and a case statement has expression <= 1. as opposed to <= 1

I'm not sure what the purpose of the 1. is. Any thoughts?

2
  • 3
    SQL Server treats the 1. literal as numeric(1,0) instead of integer due to the period. Perhaps that was specified in order to match the data type of the expression (a best practice). Personally, I'd specify 1.0 in that case.
    – Dan Guzman
    Commented Jul 1, 2015 at 1:55
  • The lack of any 0 after the decimal point and the fact that decimal has higher datatype precedence than int so the constant would be implicitly cast anyway if needed would make me think that it is likely a typing error rather than deliberate. Commented Jul 1, 2015 at 6:37

1 Answer 1

18

1. is a NUMERIC (or DECIMAL) constant, while 1 is an INTEGER constant. In some cases it is useful to specify the data type of a constant explicitly to avoid an unnecessary (or undesirable) implicit type conversion.

Consider, for example

create table t(f1 int);
insert into t values (2);

Then select 1/f1 from t returns 0 (INTEGER), while select 1./f1 from t returns 0.5 (DECIMAL).

One might assume that on the left side of the comparison in your example there is a DECIMAL column, and explicitly specifying a DECIMAL constant could marginally improve performance by avoiding an implicit type cast.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.