domingo, 7 de julio de 2013

Formatting the current date in Oracle OSB's XQuery

There's  no  format-dateTime() in Oracle OSB's XQuery implementation (as of 11.1.1.7). So I had to come up with something like this  :-S :

declare function xf:current-formatted-dateTime()
    as xs:string {
    let $currentDate := fn:current-date()
    let $currentTime := fn:current-time() (: no seconds-from-dateTime() :-( :)
    let $year := fn:year-from-date($currentDate)
let $month := fn:month-from-date($currentDate)
let $day := fn:day-from-date($currentDate)
let $hours := fn:hours-from-time($currentTime)
let $minutes := fn:minutes-from-time($currentTime)
let $seconds := fn:seconds-from-time($currentTime)
let $monthZeroPadded :=
if($month < 10) then xs:string(concat('0', $month))
else xs:string($month)
let $dayZeroPadded :=
if($day < 10) then xs:string(concat('0', $day))
else xs:string($day)
let $hoursZeroPadded :=
if($hours < 10) then xs:string(concat('0', $hours))
else xs:string($hours)
let $minutesZeroPadded :=
if($minutes < 10) then xs:string(concat('0', $minutes))
else xs:string($minutes)
let $secondsNoMS := xs:integer(fn:substring-before(xs:string($seconds), '.'))
let $secondsNoMSZeroPadded :=
if($secondsNoMS < 10) then xs:string(concat('0', $secondsNoMS))
else xs:string($secondsNoMS)

return concat($year, '-', $monthZeroPadded, '-', $dayZeroPadded, ' ', $hoursZeroPadded, ':', $minutesZeroPadded, ':', $secondsNoMSZeroPadded)
};

It can be placed and  used this way:


Produces this kind of output: 2013-07-07 17:38:05

Here you can find a list of the supported OSB's XQuery functions: http://www.tomecode.com/download/OSB_11g_XQueryFunction_list.txt


No hay comentarios: