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
The requirement to handle expiration dates in the form "yyyyMM" is clear, but it would be helpful to elaborate on scenarios where this conversion is typically used.
ReplyDeletepulse jet bag filter in delhi
Rotary Air Locks in delhi
It's great that the function considers leap years. Have you tested edge cases, such as invalid dates like "202113"?
ReplyDeleteDust Collector Manufacturer India
Axial Flow Fans India
The function names like functx:days-in-month and functx:is-leap-year are self-explanatory, but a brief comment explaining their purpose within the code would improve readability.
ReplyDeleteDust Collector Manufacturer
Air Pollution Control System manufacturer
Adding a few more examples of input values (e.g., "201401", "201911") and their expected outputs would make it easier for readers to grasp the function's behavior.
ReplyDeleteAir Ventilation System Manufacturer
Dust collector manufacturer
The functx namespace is used for utility functions. Including a brief note on what http://www.functx.com provides would be insightful for readers unfamiliar with it.
ReplyDeletepaint booth
checkered sheet dealer in delhi
The use of $inputDate as an external variable is interesting. Could you show how this variable is passed in a real-world application?
ReplyDeletestructural-steel-tubes in gwalior
pulse jet bag filter manufacturer