-1

If I have a date in YYYYMMDD format how to validate in Xquery version "1.0", so that it checks for month and year are valid and if say they are valid then a leap year with month as 02 can have a date 29 else a max 28.

Several integration tools such as Oracle Fusion Middleware implement Xquery version "1.0" not "3.0"

  • I hope you don't intend to flood the xquery tag with more question like your recent ones. A very quick search would have given you this: http://www.xqueryfunctions.com/xq/functx_mmddyyyy-to-date.html – dirkk Dec 02 '14 at 11:21
  • I dont think that replied my question there is no validation i guess...As it says: The functx:mmddyyyy-to-date function converts $dateString into a valid xs:date value. I dont need to convert a invalid date to valid date I need it to return boolean, so as to use in a if condition. – Kaushik Bose Dec 02 '14 at 11:33
  • The difference is non-significant and to change this behavior is trivial. You seem to lack a required minimal understanding of XQuery and I would recommend to read a few tutorials/books/whatever you like first. – dirkk Dec 02 '14 at 11:40
  • he man i know i am not a Xquery-Xpath guru thats why i ask questions but still i have knowledge to say the link u mentioned is just converting date in dd * mm * yyyy where * represents any separator into format yyyy-mm-dd. – Kaushik Bose Dec 02 '14 at 11:56
  • And if you would have tested it yourself you would have noticed that if it is an invalid date (like 02/29/2010) it throws an error, otherwise it does not. Now its up to you to figure out how to catch the error and simply translate this to `true()`/`false()` – dirkk Dec 02 '14 at 11:59
  • I don't expect you to be a guru, but I do expect you to do some basic research yourself and test for yourself. We are not here to provide all the code for you. – dirkk Dec 02 '14 at 12:00
  • Ya i got it now @dirkk will try to do research first Thanks – Kaushik Bose Dec 02 '14 at 12:11

1 Answers1

1

First of all, although your use case is rather simple: generally avoid tampering with dates (and especially times) on your own, and always rely on already implemented functionality, handling date and time is horribly complicated if you want to do it right.

Convert the string to a representation that XQuery's date functions are able to parse. If you can construct a date from the string, it is valid; if an error occurs, it is invalid. Dates can be constructed using xs:date($string). One way to convert the string would be using using substring(...) and string-join(...):

  xs:date(string-join((substring($date, 1, 4), substring($date, 5, 2), substring($date, 7, 2)), '-'))

To catch an error, use a try/catch block.

Community
  • 1
  • 1
Jens Erat
  • 37,523
  • 16
  • 80
  • 96
  • Xquery version "1.0" does not supports try/catch block and worse OSB implementation doesn't even support castable as – Kaushik Bose Dec 30 '14 at 10:31