Add a dynamic day value to a date in xquery - xquery

I am trying to find the date of "sunday" in the current week. This is the code, I have:
let $today := fn:current-date()
let $day-week := functx:day-of-week($today)
let $start-date := xs:date($today)-xs:dayTimeDuration($day-week)
$today -> has today's date
$day-week -> is 5 (value for friday)
I thought, when I did $today-5, I will get 7/14/2013.
But, this didn't work. Kindly correct my code. Your help is appreciated.

functx:day-of-week($today) returns an xs:integer which won't cast into an xs:dayTimeDuration. Just multiply that integer by a valid duration instead of casting:
let $today := fn:current-date()
let $day-week := functx:day-of-week($today)
let $start-date := xs:date($today)-($day-week*xs:dayTimeDuration('P1D'))

I think xs:dayTimeDuration needs a bit more setting up. Try it something like this:
let $today := fn:current-date()
let $day-week := functx:day-of-week($today)
let $start-date := xs:date($today)-(xs:dayTimeDuration('P1D')*$day-week)
Get more information here:
http://pic.dhe.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z10.doc.xml%2Fsrc%2Ftpc%2Fdb2z_xsdaytimeduration.htm

Related

How to sort by dynamically with ascending or descending in Marklogic?

let $sortelement := 'Salary'
let $sortby := 'ascending'
for $doc in collection('employee')
order by $doc/*[local-name() eq $sortelement] $sortby
return $doc
This code throws and error, what is the correct way to do this?
If you are just looking to build the order by dynamically within a FLWOR statement, you can't. As Michael Kay points out in the comments, you could use a conditional statement and decide whether or not to reverse() the ascending (default) sorted sequence.
let $sortelement := 'Salary'
let $sortby := 'ascending'
let $results :=
for $doc in collection('employee')
order by $doc/*[local-name() eq $sortelement]
return $doc
return
if ($sortby eq 'descending')
then reverse($results)
else $results
Depending upon how many documents are in your collection, retrieving every document and sorting them won't scale. It can take a long time, and can exceed memory limits for expanded tree cache.
If you have indexes on those elements, then you can dynamically build a cts:index-order() and specify as the third parameter for cts:search() in order to get them returned in the specified order:
let $sortelement := 'Salary'
let $sortby := 'ascending'
return
cts:search(doc(),
cts:collection-query("employee"),
cts:index-order(cts:element-reference(xs:QName($sortelement)), $sortby)
)

SQL database data into an array

I currently have a program which connects to a database and displays the data on a DBGrid, however I need to extract this data so I can use it in another algorithm.
When I use the command: select scores from quiz
It shows the values on the screen. (23,55,64)
How do I get these values into an array so that
[0]=23
[1]=55
[2]=64
Thanks in advance.
Best to use TList (which is an array wrapper object). Below is an excerpt from some code that I use. TSomeRect is a record to store the field data of each row in.
function CreateQuery(pConnection: Tsqlconnection; pTransaction: TSQLTransaction): TSQLQuery;
begin
result := TSQLQuery.Create(nil);
result.Database := pConnection;
result.Transaction := pTransaction
end;
var
connect: TSQLite3Connection;
SQLQuery1: TSQLQuery;
transact: TSQLTransaction;
Query : TSQLQuery;
lst :TList<TSomeRect>;
rec :TSomeRect;
begin
lst :=TList<TSomeRect>.create;
connect:=TSQLite3Connection.create(nil);
connect.LoginPrompt := False;
connect.DatabaseName := 'c:\path\to\database.sqlite';
connect.KeepConnection := False;
transact:=TSQLTransaction.create(nil);
transact.action:=caNone;
transact.database:=connect;
connect.Transaction:=transact;
Query := CreateQuery(Connect, Transact);
Query.SQL.Text := 'select * from table';
Connect.Open;
Query.Open;
while not Query.Eof do
begin
rec.field1:= Query.FieldByName('field1').AsInteger;
rec.field2:= Query.FieldByName('field2').Asstring;
lst.add(rec);
Query.Next;
end;
Query.Close;
Connect.Close;
Query.Free;
Transact.Free;
Connect.Free;

Getting error [PLS-00382: expression is of wrong type]

I have a stored procedure which is driving me nuts. I know it's a pretty simple thing, but I think I am not able to figure it out.
I am getting the error
"PLS-00382: expression is of wrong type"
in below line:
-- Write the result into Log
v_LogText := 'Summary Elapsed Time: ' || TO_CHAR(floor((cast(SYSTIMESTAMP as date) - Cast(v_StartTime as date)) * 86400)) || 'sec Batchsize ' || TO_CHAR (v_BatchSize);
Std.Log (v_WorkerName,'001','CAS', '0', 'en', ' 00000000', v_LogText );
Declarations:
v_LogText NVARCHAR2(2000);
v_BatchSize NUMBER(10,0) := iv_BatchSize; [ and iv_BatchSize is passed in the procedure as a paramenet like "iv_BatchSize IN NUMBER DEFAULT NULL"]
v_StartTime DATE := SYSDATE;
I already tried to change SYSTIMESTAMP to SYSDATE but no luck.
Assuming the amount of information provided, please check below way to do it. Hope this works for you.
declare
v_LogText NVARCHAR2(2000);
v_BatchSize NUMBER(10,0) := 76363;
v_StartTime DATE := SYSDATE;
a varchar2(100);
begin
select TO_CHAR(floor((cast(SYSTIMESTAMP as date) - Cast(v_StartTime -1 as date)) * 86400))
into a
from dual;
v_LogText := 'Summary Elapsed Time: ' || a || 'sec Batchsize ' || TO_CHAR (v_BatchSize);
dbms_output.put_line(v_LogText);
end;

Does member of function work with collection of records in oracle?

I have a index by table of records. Can I use member of function to check if a particular records exist in by PLSQL table or not.
DECLARE
TYPE emp_name_rec is RECORD(
firstname varchar2(10),
lastname varchar2(10),
hiredate varchar2(10));
TYPE staff IS TABLE OF emp_name_rec;
members staff := staff();
rec emp_name_rec;
rec1 emp_name_rec;
BEGIN
rec.firstname := 'peter';
rec.lastname := 'dunn';
rec.hiredate := 'x';
rec1.firstname := 'mary';
rec1.lastname := 'dunn';
rec1.hiredate := 'y';
members.extend;
members(members.last) := rec;
members.extend;
members(members.last) := rec1;
if rec member of members then
dbms_output.put_line('Yes its there');
else
dbms_output.put_line('no its not');
end if;
END;
This doesn't work for me, and I get no error, except I get disconnected... It looks like it compiles but it crashes my session.
I get disconnected when I try your code in Oracle 11g. => It doesn't work! as is
From http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/collections.htm ,
You can check whether a collection is null. Comparisons such as greater than, less than, and so on are not allowed. This restriction also applies to implicit comparisons. [...]
Solution is: write your code:
[..] you must define your own notion of what it means for collections to be greater than, less than, and so on, and write one or more functions to examine the collections and their elements and return a true or false value.
Thus you can't use MEMBER OF directly, but define your own cmp_rec function (which is quite obvious there, but not for Oracle it seems):
DECLARE
TYPE emp_name_rec is RECORD(
firstname varchar2(10),
lastname varchar2(10),
hiredate varchar2(10));
TYPE staff IS TABLE OF emp_name_rec;
members staff := staff();
rec emp_name_rec;
rec1 emp_name_rec;
rec_is_found boolean :=false;
function cmp_rec ( a emp_name_rec, b emp_name_rec)
return boolean is
begin
return a.firstname=b.firstname and a.firstname=b.firstname and a.hiredate=b.hiredate;
end cmp_rec;
BEGIN
rec.firstname := 'peter';
rec.lastname := 'dunn';
rec.hiredate := 'x';
rec1.firstname := 'mary';
rec1.lastname := 'dunn';
rec1.hiredate := 'y';
members.extend;
members(members.last) := rec;
members.extend;
members(members.last) := rec1;
-- we must loop over the collection ourselves
-- - I choosed to look it all, but we could break the loop when found
for i in 1..members.count loop
dbms_output.put_line(members(i).firstname);
if cmp_rec(members(i), rec) then
rec_is_found:=true;
end if;
end loop;
-- if rec member of members then
-- becomes
if rec_is_found then
dbms_output.put_line('Yes its there');
else
dbms_output.put_line('no its not');
end if;
END;

Delphi xe5 (FireMonkey) - Writing too slow to SQLite

I'm making an cross-platform application that should write values to a SQLite database. Right now it writes really slow and it freezes after a while.
I'm calling this code each 10ms:
SQLQueryInsert.SQL.Text := 'insert into task (id, latitude) values (:id, :lat)';
SQLQueryInsert.Prepared := true;
SQLQueryInsert.ParamByName('id').AsInteger := 1;
SQLQueryInsert.ParamByName('latitude').AsFloat := 12332145;
SQLQueryInsert.ExecSQL();
Any suggestions?
before you start, do this (create the vars ParamId and ParamLat in a suitable location)
SQLQueryInsert.SQL.Text := 'insert into task (id, latitude) values (:id, :lat)';
SQLQueryInsert.Prepared := true;
ParamId := SQLQueryInsert.ParamByName('id');
ParamLat := SQLQueryInsert.ParamByName('latitude');
In the code you execute everytime:
ParamId.AsInteger := 1;
ParamLat.AsFloat := 12332145;
SQLQueryInsert.ExecSQL();

Resources