Monday, July 11, 2011

XQuery, convert a dateTime into a date

Stealing almost all the code from I managed to convert a dateTime into a date (dateTimeToDate)...

guys, this is insane...

declare namespace functx = "";

declare function functx:repeat-string
( $stringToRepeat as xs:string? ,
$count as xs:integer ) as xs:string {

string-join((for $i in 1 to $count return $stringToRepeat),
} ;

declare function functx:pad-integer-to-length
( $integerToPad as xs:integer? ,
$length as xs:integer ) as xs:string {

if ($length < string-length(string($integerToPad))) then error(xs:QName('functx:Integer_Longer_Than_Length')) else concat (functx:repeat-string( '0',$length - string-length(string($integerToPad))), string($integerToPad)) } ; declare function functx:date ( $year as xs:integer , $month as xs:integer , $day as xs:integer) as xs:date { xs:date( concat( functx:pad-integer-to-length(xs:integer($year),4),'-', functx:pad-integer-to-length(xs:integer($month),2),'-', functx:pad-integer-to-length(xs:integer($day),2))) } ; declare function functx:dateTimeToDate($dateTime1 as xs:dateTime) as xs:date {
functx:date(fn:year-from-dateTime($dateTime1), fn:month-from-dateTime($dateTime1), fn:day-from-dateTime($dateTime1))

There is this alternative way, slightly simpler :o)


(thanks David for the suggestion, it helps a lot!)


David Lee said...

Vastly easier way ...


Pierluigi Vernetto said...

thanks David, excellent tip

chuck said...

Brilliant advice!