How do I define a Nullable DateTime variable in Visual COBOL ?

The DateTime fields I am inserting into my database can sometimes be null.  I want such fields in the table to show as null, not as "0001-01-01 12:00 AM".  You do this In C# by using DateTime? as your data type.  How do you do this in Visual COBOL ?  I haven't been able to find any documentation on it.

Tags:

  • What are you using to access your database in Visual COBOL?

    If you are using OpenESQL for embedded SQL support for ADO.NET then you would simply set your null indicator variable to -1 to indicate that a null should be inserted.

    Example:

               EXEC SQL 
                 CONNECT TO 'SQLADO32' 
               END-EXEC 
               move -1 to my-table-mydate-null
               move "chris" to my-table-item-a
               EXEC SQL 
                  UPDATE MY_TABLE  
                    SET MYDATE = :MY-TABLE-MYDATE:MY-TABLE-MYDATE-NULL
                       WHERE (ITEM_A = :MY-TABLE-ITEM-A)
               END-EXEC 
               exec sql commit end-exec
    
    If you are accessing the ADO.NET Framework cl;asses directly then Null values are represented by DBNull.Value

  • I can't use the above code because I'm not coding embedded SQL.

    I'm using the npgsql .NET data provider that comes with Postgresql.  Npgsql.dll includes a Parameters class for passing values to Postgresql.

            01  Closed_Month              type DateTime.  

    I need to be able to set Closed_Month to a null value when closed month is blank in my data.

    I add a parameter to the Parameters class and indicate its Postgresql data type:

    ("value5" is from my SQL INSERT string and it  represents the closed month column in the database table)

              invoke command::Parameters::Add(new type NpgsqlParameter("value5", type NpgsqlTypes.NpgsqlDbType::Timestamp)).

    Then I assign my COBOL variable to it:

              set command::Parameters[4]::Value to Closed_Month.

    I need to use a Visual COBOL variable that is like "DateTime?    Closed_Month" in C#, one that will accept a null value without converting it to the first date of the Gregorian calendar.

    Is there a way to declare a nullable type in Visual COBOL ?

  • Verified Answer

    The System.Nullable generic should do what you want. You can use that in COBOL with code such as:-

           program-id. Program1 as "Nullable.Program1".

           data division.
           working-storage section.
           01  ws-datetime     type System.Nullable[type System.DateTime].

           procedure division.
          
               set ws-datetime to new type System.Nullable[type System.DateTime]
               display ws-datetime::HasValue
               if ws-datetime::HasValue
                   display "DateTime has Value."
               else
                   display "DateTime is NULL."
               end-if              
               set ws-datetime to type System.DateTime::Now
               display ws-datetime::HasValue
               if ws-datetime::HasValue
                   display "DateTime has Value."
               else
                   display "DateTime is NULL."
               end-if              

               goback.
              
           end program Program1.

    The HasValue property indicates if the type variable is null.

     

     

  • Thanks.  This worked for updating my database.

    Phil Levin