databasecrystal-reportscrystal-reports-formulas

Format a date and time in crystal report


My date is stored in a type date column in dd/mm/yyyy format. I want to print the date in yyyymmdd format. When i used the following formula

tonumber(totext(db.colname,'YYYYMMDD'))

It gave me a "the string is non numeric" error when previewing the report.

Secondly,

My time is stored in a string column in 12 hour format. I want to display it as hh24miss format.

How do i do that ?


Solution

  • First of all, you should NOT store dates/times as text in your database. Use the apropriate datatype of your DBMS.
    Otherwise you will very likely have further problems because of this.

    When you've changed the datatype you could just drag&drop the database-field inside your report and use the formatting options of Crystal Reports to get the desired format.

    If for any reason (I doubt there's a good one) you can't change the datatype, use the following formula.

    ToText(Date({db.colname}), "yyyyMMdd")
    

    This formula converts the string to a date and then formats the date with yyyyMMdd format.
    Notice the uppercase M which is used for the month. Lowercase m is used for minutes.