Oracle Analytics Cloud (OAC) offers powerful in-house data profiling capabilities. This blog describes how to convert a column from an attribute field  to a DateTime field

If this blog is the first resource you’re looking at, please check whether the Convert to Date option described in the Oracle Documentation solves your issue. For many situations, it takes just a few clicks for Oracle Analytics Cloud to detect the DateTime format and fix your column. Sometimes, Oracle Analytics Cloud doesn’t detect a format and that’s where this blog will come in handy.

Unsupported DateTime Formats

First, let’s talk about what doesn’t work. The following table shows the attribute formats that Oracle Analytics Cloud doesn’t convert to DateTime. The left column lists the unsupported formats, and the right column shows you format characters that you might have seen in other guides or programming languages.

Unsupported Formats

Unsupported Format Characters

Full Month Name

 

20th Century/21st Century years

MONTH

MMMM

RR

Full Day Name

Day of Week

Day of Year

Abbreviated Day Name (Ex: Tues)

DAY, DDDD

D

DDD

DY

Week of Year

Week of Month

WW

W

Quarter

Q

Single Year Digit

3 Year Digit

Year Spelled out

Y

YYY

YEAR

Era Indicators (Unless considered Text)

AD, BC

Time zone Information

TZD, TZM, TZR, TZH

Converting Columns Using “Convert to Date”

Now let’s look at the steps to convert a column attribute to DateTime and the formats that do work. Let’s start with the simplest conversion method – Convert to Date.

  1. In Oracle Analytics Cloud, navigate to the column that you wish to change in the data pane.
  2. Right-click and select Convert to Date

Table

Description automatically generated

  1. A custom option dialog displays with an empty box. In the empty box, enter the date format that you want to use. Refer to the tables below to determine the formats you can enter in the box.
  2. Click Add Step to convert your column to a DateTime field.

Format Characters

Meaning

Data Type

Example

/

,

.

;

:

Punctuation

text

12:12

06/13

etc.

M

MM

MMM

month in year

text or number

If the month is July: M displays 7

MM displays 07

MMM displays Jul

d

day in month

number

10

H

hour in day (0-23)

number

22

m

minute in hour

number

30

s

second in minute

number

25

S

millisecond

number

978

y

year

number

yy displays last two digits: 05, yyyy displays all 4: 2005

literal text

text

Date(“dd-MMM-yy ‘is the‘ F ‘the‘ E ‘of‘MMM”) = 24-Dec-02 is the 4th Tue of Dec

‘ ‘

two single quotes to produce one single quote

text

Date(“k:mm ‘O”Clock’ a”): 6:15 O’clock PM

 

Examples – Convert to Date

Example Date

Corresponding Format

10-Apr-16 00:43:10 .325

dd-MMM-yy HH:mm:ss .SSS

02/11/2019 at 9:45

MM/dd/yyyy ‘at’ HH:mm

6:30 Mar/4/2001

HH:mm MMM/dd/yyyy

8/11/99

dd/MM/yy

13:30:13

HH:mm:ss

01-01-2000

MM-dd-yyyy

13/12/2022 12:00

dd/MM/yyyy HH:mm

2021-06-12T8:13:10

yyyy-MM-dd’T’HH:mm:ss

041122

MMDDYY

Converting Columns Using “Edit Column”

The Edit Column method offers less formatting options but greater control when dealing with inconsistent data. This method requires you to use the To_DateTime() function.

  1. As before, navigate to the column that you wish to change.
  2. Right-click, and this time select Edit.
    Graphical user interface, application

Description automatically generated
  3. The Edit Column dialog is displayed with a text box that contains the name of your column in blue. In this box, move your cursor before the name of your column and type: To_DateTime(
    A picture containing graphical user interface

Description automatically generated
  4. After your column name, add a comma followed by the DateTime format you want to use enclosed within two single quotes. This table shows the formats you can use.

 

Format Characters

Meaning

Data Type

Example

/

,

.

;

:

Punctuation

text

12:12

06/13

etc.

DD

Day of Month

number

14

MM

Month in year

number

06

YY

YYYY

Last 2 digits of year

Full year number

number

99

1999

hh

hour in am/pm (1-12)

number

11

mi

minute in hour

number

30

ss

second in minute

number

25

ap

am/pm identifier

text

pm

To_DateTime() Enhancers

Before showing examples of the To_DateTime() function, let’s touch on the enhancers. In general, enhancers are the reason why you use the “edit column” method rather than the “convert to date” method. When you type the To_DateTime() function you have the option to add Allow_Variable_Digits and (or) Null_On_Error to the end of the function. For example:

To_DateTime(Column Name, 'format characters', Allow_Variable_Digits, Null_On_Error) 

Let’s explore what these options do and see some examples.

Allow_Variable_Digits

This enhancer allows the algorithm to correctly assign one-digit formats correctly, even if they’re in the same column. For example, a column containing 06/4/2017 and 06/04/2017 will come through as the same date.

Null_On_Error

This enhancer allows you to discard rows that don’t fit the format you declare. For example, if row 1 was 06/4/2017, row 2 was 06/5/2017, and row 3 was July/6/2017, using the format code MM/D/YYYY would properly convert row 1 and 2 but leave row 3 empty (only for this specific column, that is, it won’t remove data from the same row in other columns).

Caution: The modifier Null_On_Error can lead to data loss. You can avoid this data loss if you use other functions such as CAST and Replace. One of these alternatives is better than the other, as discussed below.

Examples – Edit Column

Example

Example Code

06/14/2017 12:12:12 AM

To_DateTime(Name of Column, ‘MM/DD/YYYY hh:mi:ss ap’)

8/11/99

To_DateTime(Name of Column, ‘DD/MM/YY’)

13:30:13

To_DateTime(Name of Column, ‘hh:mi:ss’)

04/16/2017 

04/2/2017

To_DateTime(Name of Column, ‘MM/DD/YYYY’, Allow_Variable_Digits)

2016/02/02

2016/02/03

20!6/02/04

To_DateTime(Name of Column, ‘YYYY/MM/DD’, Null_On_Error)

2016/02/02

2016/02/03

20!6/02/04

16/02/05

To_DateTime(Name of Column, ‘YYYY/MM/DD’, Allow_Variable_Digits, Null_On_Error)

Avoiding Data Loss

Casting

The first function is CAST(). Casting is defined as changing a data type of a value (or a null value) to another data type. In Oracle Analytics Cloud, the function is listed as: CAST(expr AS type), where expr is any expression and type is any data type. A practical example here could be using it instead of To_DateTime(), in the following format:

CAST(SalesDate AS DATE)

It’s worth noting that the CAST() function isn’t nearly as comprehensive as the other methods already outlined in this blog. In most situations, these methods are more effective than CAST().

Replace

The second and most effective method of conversion is to use the Replace option. The Replace option lives in the same menu as Convert to Date and Edit. To access this option, right-click on your column and select Replace.

In the Replace dialog, you see four options:

  • Match partial values: Replaces all instances of an original string with a new string. For example, if you enter “male” for String to replace and “M” for New String, Oracle Analytics Cloud changes “male” to “M” and “female” to “feM”.
  • Match entire values only: With this option, if you enter “male” for String to replace and “M” for New String, Oracle Analytics Cloud changes “male” to “M” but doesn’t replace “female” with “feM”.
  • Use regular expressions: Uses Regex. Oracle Analytics Cloud uses Perl Compatible Regular Expression. You can find more information on how to use Regex here.
  • Replace null or missing values: The option isn’t useful in relation to this blog but it allows you to fill null or empty rows with a value.

Replace is going to be the most useful by helping you transform strings from unconvertable DateTime formats into convertible formats. For example, starting with the string “March 08/21” and turning “March “ into “03/”. Thus, transforming the row into “03/08/21”.

Conclusion

This blog is shorter than I originally intended but I hope it was useful. For more information, check out our Oracle Analytics Cloud resources:

Or  chat with a member of our team!