LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Date field formatting - need assistance

    Employee
    Posted 11-05-2014 21:43

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: mlr

    Hi,

    I have a huge dataset (~20M records) where in a column 'Closing_Date' has date values in different formats. The data source is in a csv file, which I'm reading via csv node and the column has type of string.

    I want to convert all these different formats into "MM/DD/YYYY" format. Is there an inbuilt function or if anybody has examples to achieve this? I couldn't get the date() function to perform this.

    The need to convert is that later in the flow, I need to validate if that date falls between a certain date range.

    Closing_Date
    10/31/2013
    10/5/2013
    10/7/2013
    11/02/2013
    11/1/2013
    11/11/2013
    11/15/2013
    1/3/2013
    1/31/2013
    1/5/2013
    1/7/2013
    01/02/2013
    01/15/2013
    02/02/2013
    8/07/2013
    9/01/2013
    9/11/2013
    1-SEP-2013
    15-APR-13
    30-DEC-12
    22-JAN-2014
    ===>




    ===>




    ===>




    ===>




    Closing_Date
    10/31/2013
    10/05/2013
    10/07/2013
    11/02/2013
    11/01/2013
    11/11/2013
    11/15/2013
    01/03/2013
    01/31/2013
    01/05/2013
    01/07/2013
    01/02/2013
    01/15/2013
    02/02/2013
    08/07/2013
    09/01/2013
    09/11/2013
    09/01/2013
    04/15/2013
    12/30/2012
    01/22/2014


  • 2.  RE: Date field formatting - need assistance

    Employee
    Posted 11-06-2014 04:48

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ThomasT

    This is very messy so I guess the Lavastorm guys have a better solution, but this should work


    #corrects the year to 4 digits
    if Closing_Date.right(4).left(2) == "20" then P = Closing_Date else if
    Closing_Date.right(3).left(1) == "-" then P = Closing_Date.left(Closing_Date.len()-3)+"-20"+Closing_Date.right(2) else
    P = Closing_Date.left(Closing_Date.len()-4)+"-20"+right(Closing_Date,2)

    #replaces the letters to the corresponding month
    if P.strFind("JAN") > -1 then X = P.replace("JAN","1") else if
    P.strFind("FEB") > -1 then X = P.replace("FEB","2") else if
    P.strFind("MAR") > -1 then X = P.replace("MAR","3") else if
    P.strFind("APR") > -1 then X = P.replace("APR","4") else if
    P.strFind("MAY") > -1 then X = P.replace("MAY","5") else if
    P.strFind("JUN") > -1 then X = P.replace("JUN","6") else if
    P.strFind("JUL") > -1 then X = P.replace("JUL","7") else if
    P.strFind("AUG") > -1 then X = P.replace("AUG","8") else if
    P.strFind("SEP") > -1 then X = P.replace("SEP","9") else if
    P.strFind("OCT") > -1 then X = P.replace("OCT","10") else if
    P.strFind("NOV") > -1 then X = P.replace("NOV","11") else if
    P.strFind("DEC") > -1 then X = P.replace("DEC","12") else
    X = P


    #convert do date depending on the format
    if X.strFind("/") > -1 then Newdate = date(X,"M/D/CCYY") else Newdate = date(X,"D-M-CCYY")

    #emit new field
    emit * override emit Newdate as "Closing_Date"


  • 3.  RE: Date field formatting - need assistance

    Employee
    Posted 11-06-2014 06:05

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: andycooper

    Thanks for the response Thomas, there is indeed a shorter way of achieving the conversion. If you paste the details below into a filter node this will convert all the values into the correct format.

    if 'Closing_Date'.isDate("D/M/CCYY") then
    newDate = 'Closing_Date'.date("D/M/CCYY")else
    if 'Closing_Date'.isDate("M/D/CCYY") then
    newDate = 'Closing_Date'.date("M/D/CCYY")else
    if 'Closing_Date'.isDate("D-m-CCYY") then
    newDate = 'Closing_Date'.date("D-m-CCYY")else
    if 'Closing_Date'.isDate("D-m-YY") then
    newDate = 'Closing_Date'.date("D-m-YY")else
    newDate = null #or whatever you want to do with dates that cannot be converted.
    emit *, newDate

    If you encounter any additional date formats then you would need to add additional lines to the filter script. This also includes an else statement to add "NULL" to values it can't convert so you can easily find records that need checking.

    Ta

    Andy


  • 4.  RE: Date field formatting - need assistance

    Employee
    Posted 11-09-2014 23:02

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: mlr

    Thanks to both Thomas & Andy for your help. I was able to get the dates in desired format.