transforming date from xml to csv using dataweave - date

I want to transform a date which is attribute coming from xml file into csv format using dataweave. now, transformation rule is 'If date is Sunday, make the date minus 6 days'. otherwise keep as it is.

If your input is something like this -
<?xml version='1.0' encoding='UTF-8'?>
<root>
<createDate>2016-05-08</createDate>
</root>
Then you can use below dataweave code to get date as required. When the day is Sunday, subtract 6 days from date -
%dw 1.0
%output application/csv
---
{
row: {
date: (payload.root.createDate as :date) unless
(((payload.root.createDate as :date) as :string {format: "E"}) == 'Sun')
otherwise ((payload.root.createDate as :date) - |P6D|)
}
}
It will output as below -
date
2016-05-02

Related

How to convert Date column format in case class of Scala?

I am using Scala spark.I have two similar CSV files with 10 columns.One difference is with the Date column format.
1st file Date format yyyy-MM-dd
2nd file Date format dd-MM-yyyy
Objective is to: create seperate schema rdd for each file and finally merge both the Rdds.
For the first case class, I have used Date.valueOf [java.sql.Date] in the case class mapping.No issues here..
Am finding issue with the 2nd file Date format..
I have used the same Date.valueOf mapping..but it's throwing error in the date format...
How can I map the date format in the second file as like the 1st format yyyy-MM-dd? Please assist
Use java.util.Date:
val sDate1="31/12/1998"
val date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1)
import java.text.SimpleDateFormat
Result:
sDate1: String = 31/12/1998
date1: java.util.Date = Thu Dec 31 00:00:00 CET 1998
to change the output format as a common string format.
val date2=new SimpleDateFormat("yyyy/MM/dd")
date2.format(date1)
Result:
res1: String = 1998/12/31

Mulesoft add excel date serial number to date

I'm reading an excel file data, one of the column is 'DateTime'(i.e. 01/01/1990 00:00:00) but Anypoint read it as string type 'excel date serial number'(i.e. 33257.415972222225).
How do I transform in DataWeave the date correctly?
ex:
date : "01/01/1990 00:00:00" as :date {format: "MM/dd/yyyy HH:mm:ss"} + "P$(33257.415972222225/12)Y"
There is an existing answer for converting Excel dates to Unix dates (epoch time) that can be easily adapted to DataWeave. Note that Excel in Windows and Mac use a different starting date so the formula varies accordingly.
script
%dw 1.0
%output application/json
---
{
dateExcelWindows: (((payload as :number) - 25569 ) * 86400 ) as :datetime,
dateExcelMac: (((payload as :number) - 24107 ) * 86400 ) as :datetime
}
input (this is June 9th, 2011 10:30 AM in Windows Excel):
"40703.4375"`
output:
{
"dateExcelWindows": "2011-06-09T10:30:00Z",
"dateExcelMac": "2015-06-10T10:30:00Z"
}

How to convert date field to this format 2018 / 01

I have one date field in my table as char "01jan2017",
I want to convert the date field to this format "2018 / 01" and there should be space between forward slash.
Thanks
If what you're looking for is just for display then here is a character conversion
data r;
date = '01jan2017'd;
date1 = compbl(put(year(date),best.)|| " / "||put(month(date),z2.));
run;
There are three key steps you need to do:
catx(' / ',year(input(date_char,date9.)),put(month(input(date_char,date9.)),z2.));
Convert the date to date9. format in order to extract the year and month,
Use the z2. format for the month to get the leading Zero,
Use Catx() to concatinate the year , month & ' / '.
Full Code:
data want;
date_char="01jan2017";
dateYYMM=catx(' / ',year(input(date_char,date9.)),put(month(input(date_char,date9.)),z2.));
run;
Output:
date_char=01jan2017 dateYYMM=2017 / 01

teradata yymmdd date format

I have a field that should be 6 digit character but it is numeric. I am using the following code to add the leading zero:
select CAST(CAST(CHD_OPEN_DATE AS FORMAT '9(6)') AS CHAR(9))
I'm using the following code to format this as a date:
cast(cast(lpad(to_char(CHD_OPEN_DATE),6,'0') as date format 'YYMMDD') as date format 'YYYY-MM-DD')
When using this date format 1990 comes up as 2090. Is there a work-around for this?
If your number has a YYMMDD format you can use the following to cast to a date without the need to cast to an intermediate string. Assuming a date range between 1930 and 2029:
SELECT 900331 AS CHD_OPEN_DATE,
Cast(CASE WHEN CHD_OPEN_DATE < 300000
THEN CHD_OPEN_DATE + 1000000
ELSE CHD_OPEN_DATE
END AS DATE)

SAS date swap year and day

I am working with a dataset containing a date variable with the format MMDDYY10..
The problem is, that the day, month and the year have been swapped.
The data set as it looks now:
Obs Date
1 11/01/1931
2 11/06/1930
3 12/02/2003
4 12/07/2018
What I would like is a date variable with the format DDMMYY10., or a similar:
Obs Date
1 31/01/2011
2 30/06/2011
3 03/02/2012
4 18/07/2012
Observation 1 is hence written as the 1st of November 1931, but really it is the 31st of January 2011.
Does anyone know how I can change this?
Looks like you read the original raw data using the wrong INFORMAT. Most likely you had data in YYMMDD format and you read it as MMDDYY. You can use the PUT() and INPUT() functions to attempt to reverse it.
data have ;
input date mmddyy10.;
newdate = input(put(date,mmddyy6.),yymmdd6.);
format date newdate yymmdd10. ;
put (date newdate) (=);
cards;
11/01/1931
11/06/1930
12/02/2003
12/07/2018
;;;;
Results:
date=1931-11-01 newdate=2011-01-31
date=1930-11-06 newdate=2011-06-30
date=2003-12-02 newdate=2012-02-03
date=2018-12-07 newdate=2012-07-18