OSB 12c Xquery function to convert the date of credit card expiry date to yyyy-MM-dd
I had a requirement to convert the credit card expiration
date in the form of “201506” to “yyyy-MM-dd”.
The challenge here is adding the day value, we have to add
the last day of the respective month.
For example, if we get 201502, the converted date should be “2015-02-28”
Here is the function to convert this date format.
Xquery:
xquery version "1.0" encoding "utf-8";
(:: OracleAnnotationVersion "1.0" ::)
declare namespace functx = "http://www.functx.com";
declare variable $inputDate as xs:string external;
declare function functx:days-in-month
( $date as xs:anyAtomicType? ) as xs:integer? {
if (month-from-date(xs:date($date)) = 2 and
functx:is-leap-year($date))
then 29
else
(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
[month-from-date(xs:date($date))]
} ;
declare function functx:is-leap-year
( $date as xs:anyAtomicType? ) as xs:boolean {
for $year in xs:integer(substring(string($date),1,4))
return ($year mod 4 = 0 and
$year mod 100 != 0) or
$year mod 400 = 0
} ;
declare function local:func($inputDate as xs:string) as xs:string {
concat((fn:substring(fn:data($inputDate),1,4)),'-',
(fn:substring(fn:data($inputDate),5,2)),'-',(functx:days-in-month(concat(fn:substring(fn:data($inputDate),1,4),'-',
fn:substring(fn:data($inputDate),5,2),'-',15))))
};
local:func($inputDate)
(:: OracleAnnotationVersion "1.0" ::)
declare namespace functx = "http://www.functx.com";
declare variable $inputDate as xs:string external;
declare function functx:days-in-month
( $date as xs:anyAtomicType? ) as xs:integer? {
if (month-from-date(xs:date($date)) = 2 and
functx:is-leap-year($date))
then 29
else
(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
[month-from-date(xs:date($date))]
} ;
declare function functx:is-leap-year
( $date as xs:anyAtomicType? ) as xs:boolean {
for $year in xs:integer(substring(string($date),1,4))
return ($year mod 4 = 0 and
$year mod 100 != 0) or
$year mod 400 = 0
} ;
declare function local:func($inputDate as xs:string) as xs:string {
concat((fn:substring(fn:data($inputDate),1,4)),'-',
(fn:substring(fn:data($inputDate),5,2)),'-',(functx:days-in-month(concat(fn:substring(fn:data($inputDate),1,4),'-',
fn:substring(fn:data($inputDate),5,2),'-',15))))
};
local:func($inputDate)
Hope it helps.
It 's an amazing and awesome blog
ReplyDeleteOracle SOA Online Training