Why is the date field saved as zero?

advertisements

Why @OpeningDate is getting saved as NULL even though I am doing this.

 PROCEDURE [dbo].[InsertCaseANDHearingDetails]

    @HearingDate    datetime,

    @IsOpeningDate  bit= null,
    @OpeningDate date= null,

AS
Begin
      IF(@IsOpeningDate = 0)
      Begin
          Set @OpeningDate= (Select Convert(varchar, @HearingDate, 106))
      End

      Insert Into Hearings
      values (@HearingDate, @OpeningDate)

End

Even though I am calculating it and hearing date is not null but why OpeningDate is getting saved as NULL.


@HearingDate != NULL will not work. The result of this comparison is always unknown. Use @HearingDate is not null instead.

Because the variable @HearingDate is not initialized, it would have a null value.

Also, the variable @OpeningDate wouldn't be set to Select Convert(varchar, @HearingDate, 106) because the if condition evaluates to unknown.

Hence, when you select values from the table they would be null.

Edit:

@IsOpeningDate  bit= null
...
IF(@IsOpeningDate = 0)

This condition evaluates to unknown too as this is doing 0 = null. You cannot compare with null.