locked
Using operators on NULL RRS feed

  • Question

  • User-1193791088 posted

    If I have values in a Stored Procedure and they are NULL will I get an error or will the operation be NULL

    DECLARE @HeightCm DECIMAL (18, 4) = @HeightIn * 2.54
    DECLARE @WidthCm  DECIMAL (18, 4) = @WidthIn * 2.54
    DECLARE @DepthCm  DECIMAL (18, 4) = @DepthIn * 2.54
    DECLARE @LengthCm  DECIMAL (18, 4) = @LengthIn * 2.54
    DECLARE @WeightKg DECIMAL (18, 4) = @WeightOz * 0.0283495
    DECLARE @SizeLt DECIMAL (18, 4) = @SizeOz *	0.0295735

    If @HeightIn IS NULL will @HeightCm then be null, or will an error occur when i try to run the multiplication?

    Do I need to add

    IF (@HeightIn IS NOT NULL)

    BEGIN

    Do Here....

    END

    Wednesday, March 16, 2016 2:19 PM

Answers

  • User1633621018 posted

    No, there will be no error but you will always get null. So yes, you should code to handle null also.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 16, 2016 2:26 PM
  • User77042963 posted

    It will not error out in your code and you will get null from the calculation.

    But you need to handle the NULL value properly for calculation based on your requirement.

    You can assign a value to the variable if it is NULL. For example, use ISNULL(@HeightIn,0).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 16, 2016 2:27 PM
  • User753101303 posted

    Hi,

    Using NULL in a expression gives NULL so it will be fine if this is what you want (what if you just try ?). Note though that AFAIK you are not allowed to do the assigment with the declaration in SQL (not sure if you ask because you do have a problem ???!!!)

    DECLARE @HeightIn DECIMAL (18, 4)
    DECLARE @HeightCm DECIMAL (18, 4)
    SET @HeightIn=NULL
    SET @HeightCm=@HeightIn * 2.54
    SELECT @HeightCm

    shows NULL.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 17, 2016 10:53 AM

All replies

  • User1633621018 posted

    No, there will be no error but you will always get null. So yes, you should code to handle null also.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 16, 2016 2:26 PM
  • User77042963 posted

    It will not error out in your code and you will get null from the calculation.

    But you need to handle the NULL value properly for calculation based on your requirement.

    You can assign a value to the variable if it is NULL. For example, use ISNULL(@HeightIn,0).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 16, 2016 2:27 PM
  • User753101303 posted

    Hi,

    Using NULL in a expression gives NULL so it will be fine if this is what you want (what if you just try ?). Note though that AFAIK you are not allowed to do the assigment with the declaration in SQL (not sure if you ask because you do have a problem ???!!!)

    DECLARE @HeightIn DECIMAL (18, 4)
    DECLARE @HeightCm DECIMAL (18, 4)
    SET @HeightIn=NULL
    SET @HeightCm=@HeightIn * 2.54
    SELECT @HeightCm

    shows NULL.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 17, 2016 10:53 AM