Home > database >  Remove seconds from dateTime in XQUERY
Remove seconds from dateTime in XQUERY

Time:07-03

So i have a dateTime like "2022-06-25T03:00:00 02:00" and i have to just remove the seconds and retain everything else.

eg: "2022-06-25T03:00 02:00"

Below is the input xml, i have to tranform the data to match the eg: that i ahve mentioned above.

Could someone please help? Note: I am new to XQuery, so please tell me what is that your xquery doing too.

Input XML

<?xml version="1.0" encoding="UTF-8"?>
<Message xsi:schemaLocation="http://abcd.ch http://abcd.ch/ns/Message.xsd" xmlns="http://abcd.ch/ns/Message" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <messageId>4894954fyfuguyutfu_98798_98798</messageId>
 <messageType>Message</messageType>
 <messageDate>2022-06-23T12:49:11 02:00</messageDate>
</Message>

Expected output XML

<?xml version="1.0" encoding="UTF-8"?>
<Nomination xmlns="http://www.example.com" Release="1" Version="EGAS40">
    <Identification v="ABC123"/>
    <Type v="55G"/>
    <messageDate v="2022-06-23T12:49 02:00"/>
</Nomination>

CodePudding user response:

The easiest approach is using fn:replace with RegEx:

replace($file/dateTimeValue,'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}):\d{2}(\ \d{2}:\d{2})','$1$2')

Result is

2022-06-25T03:00 02:00

CodePudding user response:

Assuming your XQuery processor supports XQuery 3.0, you could use the format-dateTime() function with a picture string that excludes the seconds:

let $date := "2022-06-23T12:49:11 02:00"
return
    $date
    => xs:dateTime()
    => format-dateTime("[Y0001]-[M01]-[D01]T[h01]:[m01][Z]")

This returns "2022-06-23T12:49 02:00".

If you're not familiar with =>, see Arrow operator.

All of these links point to the current version of the official XQuery specification, XQuery 3.1.

  • Related