Highlighted
Super Contributor.
Super Contributor.
3316 views

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

Jump to solution

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.

Phil Levin

Tags (1)
0 Likes
1 Solution

Accepted Solutions
Highlighted
Absent Member.
Absent Member.

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

Jump to solution

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.

 

 

View solution in original post

0 Likes
4 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

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

Jump to solution

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

0 Likes
Highlighted
Super Contributor.
Super Contributor.

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

Jump to solution

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 ?

Phil Levin

0 Likes
Highlighted
Absent Member.
Absent Member.

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

Jump to solution

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.

 

 

View solution in original post

0 Likes
Highlighted
Super Contributor.
Super Contributor.

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

Jump to solution

Thanks.  This worked for updating my database.

Phil Levin

Phil Levin

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.