animal
animal

Reputation: 1004

convert YYYYMMDD to date format YYYY/MM/DD

I am trying to convert bigdecimal(YYYYMMDD) to date format (YYYY/MM/DD) using pig script. For that i have written below code

STOCK_A = LOAD '/user/cloudera/hl7' USING PigStorage('|');
data = FILTER STOCK_A BY ($1 matches '.*OBR.*');
MSH_DATA = FOREACH data GENERATE ToString($8,'yyyy-MM-dd')AS date;

Output i am getting is

20140926-01-01
20140929-01-01
20141002-01-01

Though the expected output is

2014/09/26
2014/09/29
2014/10/02

Sample is having date

20140926
20140929
20141002

Upvotes: 0

Views: 779

Answers (2)

user5227388
user5227388

Reputation:

MSH_DATA = FOREACH b GENERATE ToString(ToDate($7, 'yyyyMMdd','Asia/Kolkata'),'yyyy/MM/dd') AS date;

If you are working on the data from other country then you need to above code for time zone. (above example for working in India)

Upvotes: 1

Alexey
Alexey

Reputation: 2478

MSH_DATA = FOREACH data GENERATE ToString($8,'yyyy-MM-dd')AS date;

I wonder why expected output is 2014/09/26 when you provide 'yyyy-MM-dd' as date format? To achieve your goal first convert $8 ( that is chararray when you load it) to date

MSH_DATA = FOREACH data GENERATE ToString(ToDate($8, 'yyyyMMdd'),'yyyy/MM/dd') AS date;

Upvotes: 2

Related Questions