Date formatting issue in Marklogic - xquery

I am using xdmp:document-filter(doc(uri)) to fetch the metadata from the documents. When I run this command on one of the documents I get the following result:-
xdmp:document-filter(doc("/Vision.doc"))//*:meta[#name eq "Creation_Date"]/#content
<?xml version="1.0" encoding="UTF-8"?>
<results warning="attribute node">
<warning warning="attributes cannot be root nodes" content="17-05-2012 00:48:00"/>
</results>
And when I run this command on another document then I get this:-
<?xml version="1.0" encoding="UTF-8"?>
<results warning="attribute node">
<warning warning="attributes cannot be root nodes" content="2012-06-03T13:45:00Z"/>
</results>
You can see that date format is different in both the outputs. There may be different date formats in documents uploaded in Marklogic Server. But I want to show the creation date of documents in some fixed format (e.g. May 16, 2012). How can I convert the different date formats to a fixed date format ? And also I want to compare these dates to the date entered by the user. The documents matching the search criteria should get returned by the search query. So I have two questions here:-
How to convert creation date of particular documents to some fixed format and to display it in the UI.
How to compare this creation date to the date entered by the user(which is in "mm/dd/yyyy" format) so that I can get the correct result.

You will have to parse the dateTime value. For example:
let $dt := "17-05-2012 00:48:00"
return
if ($dt castable as xs:dateTime)
then xs:dateTime($dt)
else xdmp:parse-dateTime("[Y01]-[M01]-[D01] [h01]:[m01]:[s01]", $dt)
This will return an xs:dateTime atomic value, which can be compared and displayed in the UI. If you want to support additional formats, you will need to create additional parse "picture" strings so they can also be converted to xs:dateTime. See the documentation on xdmp:parse-dateTime() for more information.

As a part of a larger open source project I cooked up a date parsing library that handles at least 20 different formats in 6 different languages. You can also supply your own formats if one is not already defined. It works by feeding it a date as a string in any of the defined formats and returning an xs:dateTime if it was able to successfully parse it. You can find the library here:
https://github.com/marklogic/Corona/blob/master/corona/lib/date-parser.xqy
To use it:
import module namespace dateparser="http://marklogic.com/dateparser" at "date-parser.xqy";
dateparser:parse($filteredDocument//*:meta[#name eq "Creation_Date"]/#content)
This will allow you to normalize the various date formats that binary documents can have. I will note that different binary formats (Word, PDF, JPEG, etc) will use different names for the creation date. So just looking for the "Creation_Date" metadata could leave some holes depending on what formats you're storing in MarkLogic.
Also note, that if you just want the date information without the time portion, you can cast the xs:dateTime to an xs:date. Doing so will retain the timezone information which is likely a good thing.
As for your second question…
There is a number of different ways to do this and reading some of the MarkLogic documentation is a good place to start. I'd recommend taking a look at:
http://docs.marklogic.com/guide/search-dev/rangequery
Hopefully that will shed a bit of light on what your query needs to look like. In simplest form you will probably have to first parse the date that the user provided. This can also be done with the date parsing library so users can enter tons of different date formats (eg: November 13th, 2012 or even in Spanish Noviembre 13th, 2012). Then use that parsed date to construct date range queries in MarkLogic.
If that doesn't help I'd post another question here with the specifics of where you're getting hung up.

Related

Convert multiple gridview date fields into one format

I'm spinning my wheels right now and what I'm pretty sure is an easy answer, I just can't see at the moment. What I'm attempting to do take two sets of converted dates in a database and I want to present them in a grid view as one single date format.
Right now the two date formats are as follows:
YYYYMMDD
mon dd yyyy hh:miAM/PM
I want to convert them all into one format. For giggles, lets just convert them all to 112 (YYYYMMDD).
Now I want to present this all in the same SELECT statement that populates my gridview with other columns of data of various forms lots of text, data entry, long characters, etc.
The thing I haven't been able to wrap my head around is how to put this into a SELECT statement that includes all the rest of the information that populates the gridview.
So a normal SELECT statement (into all nvarchar(max) fields...the table is formed this way because we deal with lots of non standard data importing like Unix) would look like this (this is just a sample database):
SELECT [record_number], [price], [product], [description], [date_of_order], [customer_id] [comments]from sample_database
When presenting the 'date_of_order' field, I figured I need to use a CAST or CONVERT with the proper conversion numbers. However, after looking online I'm scratching my head on how to do this as most of the somewhat relevant information is focused around converting with the GETDATE option instead of leveraging the date that's already written to the database (the information is entered on a different part of the website).
I'm just looking for a pointer or a suggestion in the right direction to use a SELECT statement in my gridview (along with all of the other fields) that can convert any/all of the entries into one common date format. I know I'm missing something simple here but I don't know what.
I should note that I don't want to change them in the Database just when presenting them in the Gridview.
Use the following Convert SQL Function around all of the date fields you require, be warned that this can cause errors if the value is not a correctly formatted date.
CONVERT(datetime, [OldDate], 112)
Found here.
Also look here for more information on the Convert Function

CouchDB Date functions

We are in the process of converting a rather large PHP/MySQL project to Angular/Node.js/CouchDB. The main problem I am running into now is that our MySQL queries are rather complicated, using a lot of date functions (like DATE_DIFF, DATE_FORMAT, etc.) and I don't know how to convert them to this new architecture.
How do most devs handle those types of functions in CouchDB? Do they just pull the raw data from the database and leave all of the calculations up to the controller/front-end?
Example query:
SELECT DATE_DIFF(NOW(),table.datefrom) as how_long, DATE_FORMAT(table.datefrom,'%m/%d/%Y') as formatted_date FROM table ORDER BY datefrom
How would that query be handled with CouchDB?
Datetimes are not a "native" type in CouchDB. However, you have several good options that you can choose between depending on the situation.
You can use a "timestamp" numeric value. (either in the native milliseconds, or converted to seconds if needed) You can get a timestamp for "now" with (new Date()).valueOf().
You can also break up the parts of your datetimes into an array. ([ year, month, day, hour, minute, second ]) This will enable you to use grouping to "drill down" into increasingly specific time-frames as well as query based on individual parts of the date.
If you want date manipulation and formatting from a tested library, you can pull in a 3rd party module like moment.js as a CommonJS module that you can use in your view/show/list/etc.
I can see one potential issue with your example query above. You are basically getting a "seconds since" via DATE_DIFF(NOW(), ...). In a view function, you won't be able to use a "transient" value like NOW() since views need to remain unaffected by outside variables/conditions. However, this is solved by adding a list function that can take your view results and transform the output to have "relative" values like what you are trying to achieve, and can also receieve querystring arguments to further add dynamism to your view.

Control input of SQLite attribute to date format only.

I have been reading all about converting TEXT fields into date formats and ways to use Python to create date objects but my question remains.
My table has a dateTime column that is specified as TEXT, I would like to build a constraint that forces input to be in dateTime format, but as SQLite doesn't do dates (as I would like) I haven't worked out how to do it.
My current ideas: 1. Limit number of characters
2. Set separate attributes for day, month and year and constrain their domains
3. It is silly to do this on the database side just do it in the user interface
I would appreciate your opinions on these or other options.
Thanks :)
I've been trying to solve the same issue and the method I came up with was to use the date/time functions to parse the value and check that it hasn't changed. Essentially the following:
CREATE TABLE dts_test (
dts TEXT CHECK (dts IS datetime(dts))
);
If the default format is not what you want you can use the strftime or similar to design whatever format you want, but it must be something that the built-in date and time functions can parse.

ASP.NET Passing Inconsistent DateTime Format to SQL Server [duplicate]

This question already has answers here:
RDLC - "String Not Recognized As A Valid DateTime" Error
(2 answers)
Closed 2 years ago.
I know this is an age-old question but I'm not exactly new to it and I haven't found an answer which helps me so far!
I've got an ASP.NET site which displays data from an SQL Server 2005 database. At the top of the page are 'Date From' and 'Date To' textboxes which are used to filter the data shown beneath.
The data is displayed using an SqlDataSource, and the two dates are passed as parameters to a Stored Procedure. The textboxes display the dates and accept input in UK date format (dd/MM/yyyy) and it works fine.
Now I've added a new page with exactly the same setup, displaying slightly different data. In the backend I created a new Stored Procedure by copying and pasting my original one, it's almost identical. And yet on this page I get errors with my dates because they're being read as MM/dd/yyyy, meaning that today's date, for example, 15th August 2011, is passed as 15/08/2011 and isn't a valid date.
I've checked over everything and I can't understand why this should work on one page and not another, especially when I've basically just copied all of the original code and tweaked it slightly. Can anyone suggest anything I can check that I might not have thought of?
The text boxes are strings, which are in the thread local which is set from the browser (langauges).
Convert them to aa DateTime object first, using a local aware transformation, then write the dateTime object to the server. NEVER (!) deal with strings to sql server unless you format them in ISO independent form (2011-08-17 23:52:11).
But in general, ASp.NET will show dates, times, numbers in the local langauge of the browser. Either turn that off, or deal with it. It is nice for the user. So, check locales - user, server process. What is the thread current locale?
You need to use the SQL convert in stored procedure like:
convert(varchar, #yourdateParameter, 103) for format dd/mm/yyyy
or
convert(varchar, #yourdateParameter, 101) for format mm/dd/yyyy
Hope this helps.

To retrieve Date in the same format as stored

I am facing an issue in my Flex application.
I am creating some array collections and storing date objects in it.In the later part of the application I will create 'advanceddatagridcolumns' with these array collections as the data sources.
Initially while creating the Array Col , I do have the 'formats' given by user, for each array collection eg. '1995/06/25' but in the later part I have no access to these formats. I want to display this dates in the data grid in same way as the user has specified. Right now, it displays it in the default format 'Sun Jun 25 00:00:00 GMT+0530 1995' instead of '1995/06/25'.
I have a common 'labelFunction' for these advancedatagridcolumns, and thus I can not use the DateFormatter as I 'formatstring' would be different for different columns.
So is there some way to display/retrieve the date in the same format as stored and not in the default way. Or while creating the date object can't I specify that I would always like it to be returned in some desired format.
The Date class in Flex as far, as my experiences go, is not very versatile. My hunch is that this is by no means possible with the regular framework capabilities.
However, if the date does not need to be changed after the user has input it, you might just want to save it as String and only create a temporary ArrayCollection of Dates for displaying them. This way you don't need to worry about the format changing because you're saving the original value.
How are you parsing the dates anyway? I mean that are you finding out the format yourself with RegExps or such, or using the parse method of the Date class?
just create a labelfunction specific to that column only and then you can use the formatstring

Resources