Date comparison between specific date formats in xquery - xquery

I have two dates; date1 = 2021-01-14T00:00:00-05:00 and date2 = 2021-01-17T00:00:00-05:00.
is there a good way to compare them in xquery

Yes, you can simply do
xs:dateTime('2021-01-14T00:00:00-05:00')
< xs:dateTime('2021-01-17T00:00:00-05:00')

Related

How to deal with date in SQLite?

I have a table in Sqlite DB having two fields Id and Date (Date is of type Text).
I have stored a few dates in the table from c#. now i want to get the records matching specific day, month and year.
The query i have tried is:
select strftime('%m', Date) from testTbl Where id = 3;
also:
select Date(substr(Date, 0, 10)) as daa from testTbl Where id = 3;
but the result of these two quires is always null.. can anyone help me to sort this out?
Proposed (immediate) fix
Use the following select
select substr(Date, 0, 10) as daa from testTbl Where id = 3;
Cause of the issue
The problem (if you surround the above substr with a Date function) is that you're using a Text type that is not in the expected format
Time Strings
A time string can be in any of the following formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
Alternative (better) approach
Anyway IMHO, it would be better to create the column with a Date type and to insert values in the following way
insert into testTbl values (DateTime("2015-12-31"),3);
so that you'll be able to do
SELECT strftime('%m/%d/%Y',Date) from testTbl where id = 3;
or also
SELECT Date from testTbl where Date > DateTime('2016-01-01');
from C# the parameterized command would be similar to
"insert into testTbl values (DateTime(?),?);"
with a parameter value myDate.ToString("yyyy-MM-dd")
Anyway you can actually get the month string with substr(Date,0,2) and the year with substr(Date,5,4) with your current format. I'm simply suggesting an alternative that I would find more standard (using the built-in Date format)

How to convert chararray to datetime with milliseconds in pig latin

I wish to convert following value which is a chararray in pig
2016-05-11 23:59:57.628197
to
2016-05-11T23:59:57.628-05:00
How can I do it ?
Following is what I tried considering alias 'a2' contains list of datetime values in chararray in the column named 'input_date_value'
FOREACH a2 GENERATE input_date_value AS input_date:chararray,
ToDate(input_date_value,'YYYY-MM-DD HH:mm:ss.SSSSSS') AS modification_datetime:datetime;
For input -
2002-07-11 16:58:40.249764
Output is -
2002-01-11T16:58:40.249-05:00
The month values like '07' are not getting picked up,
The created timestamp has month set to 01' i.e. January everytime for all dates.
Can someone help. What am I doing wrong ?
https://pig.apache.org/docs/r0.11.1/func.html#to-date ToDate takes SimpleDateFormat only supports milliseconds http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
The -05:00 you see is the time zone ToDate is actually truncating to 3 digits as it supports only milliseconds
Use lowercase character d instead of uppercase D for parsing date values.
Now, I have managed to fix it myself on (In Pig 0.11)
Apparently Pig 0.11 does not support the date format components I used earlier for parsing the month and date.
I found below inference which hints on the incompatibility as mentioned https://www.w3.org/TR/NOTE-datetime
Use:
'YYYY-MM-dd HH:mm:ss.SSSSSS'
instead of 'YYYY-MM-DD HH:mm:ss.SSSSSS'
It now gives correct output.
Input:
2001-11-28 16:04:49.22388
Output:
2001-11-28T16:04:49.223-05:00

how to convert a string date variable into datetime variable in sas

I have a string variable for datetime. Sometimes it is a whole number like 3040000 sometimes a decimal value like this 3130215.123.
I would like to convert this into a date time variable like mm-dd-yyyy.
Thanks in advance.
add: I think the value 3130215.123 refers to feb-15-2013 12:30:00.
I think these are
3YYMMDD.HHMM
3130215.123 -> 3|13|02|15|12|30
(the last 0 is assumed).
So, you need:
length year month day hour minute second $2;
year=substr(dtvar,2,2);
month=substr(dtvar,4,2);
day=substr(dtvar,6,2);
hour=translate(subpad(dtvar,9,2),'0',' ');
minute=translate(subpad(dtvar,11,2),'0',' ');
second=translate(subpad(dtvar,13,2),'0',' ');
then
new_dtvar=dhms(mdy(month,day,year),hour,minute,second);
Dates, Times, and Datetimes are stored as doubles. Only the format matters for display.
try
var1 = input(var,best.);
format var1 datetime19.;
in a data step to apply the format. Then look at the results.
Based on comments on the original question, the statements would be:
format var_date ddmmyyd10. var_time time5. var_datetime datetime19.;
var_date=input(put(int(17000000+(var)),10.),yymmdd10.);
var_time=input(put((var-int(var))*1e6,z6.),hhmmss6.);
var_datetime=dhms(var_date,hour(var_time),minute(var_time),0);
Haven't had a chance to test, so feel free to comment with any errors you get.

Date format in SAS

I have a SAS code that I need to convert into R.
My SAS code is something like this -
proc sql;
create table data as
select a.*,b.qty from Sales as a inner join Units as b
on a.id=b.id and put(a.date,yymmn6.)=put(c.date,yymmn6.)
quit;
I know that put(a.date,yymmn6.) converts the date into a SAS date value. But what does a.date become after this function? If date=01jan2012, put(a.date,yymmn6.) makes it as some SAS value that represents 201201 or 20120101? i.e. the SAS value created will stand for the whole date or just the year and mon of the date?
Currently, I am writing the R code for this as -
data <- sqldf("select a.*,b.qty from Sales as a inner join Units as b
on a.id=b.id and a.date=c.date")
Should I be doing it as -
Sales$date <- as.yearmon(Sales$date)
Units$date <- as.yearmon(Units$date)
data <- sqldf("select a.*,b.qty from Sales as a inner join Units as b
on a.id=b.id and a.date=c.date")
I don't have access to SAS and hence, I cannot try this out on a sample data. Any help would be great. Thanks!
put(a.date,yymmn6.) converts a numeric date value to a character value stored as yyyymm (e.g. 201201). Therefore the join condition is matching all dates where the month and year are the same, but not necessarily the day.
I'm not sure of the best way of achieving this in R, but you seem to have some ideas on this.
Hope this helps.
When you use put(a.date,yymmn6.) the output of that function is a character. Put takes a numeric input and format and outputs the formatted numeric value as character. input function does the opposite.
data mydata;
sas_numeric_date = "01jan2012"d;
sas_yyyymm_char_date = put(sas_numeric_date, yymmn6.);
sas_yyyymm_numeric_date = input(sas_yyyymm_char_date, yymmn6.);
output;
sas_numeric_date = "29Feb2012"d;
sas_yyyymm_char_date = put(sas_numeric_date, yymmn6.);
sas_yyyymm_numeric_date = input(sas_yyyymm_char_date, yymmn6.);
output;
format sas_numeric_date sas_yyyymm_numeric_date date9.;
run;
sas_numeric_date sas_yyyymm_char_date sas_yyyymm_numeric_date
01Jan2012 201201 01Jan2012
29Feb2012 201202 01Feb2012
So, when you apply the yymmn6. as informat on sas_yyyymm_char_date - which itself is in yyyymm format, the resulting value is numeric and day part in the date defaults to the first day of the month as shown above.

DateAdd function in pl/sql

As the title suggested , I am looking for a function in pl /sql which does something similar like the DateAdd function.
I have been looking and I found the add_months function but I would really like one that is a little more variable since I need to be able to add minutes, hours , days etc.
It appears there's not many solutions :
PL/SQL allows you to perform arithmetic operations directly on date variables. You may add numbers to a date or subtract numbers from a date. To move a date one day in the future, simply add 1 to the date as shown below:
hire_date + 1
You can even add a fractional value to a date. For example, adding 1/24 to a date adds an hour to the time component of that value. Adding 1/(24*60) adds a single minute to the time component, and so on.
Besides adding numbers to dates - though it's the simplest way - you can add intervals like that:
date1 := date2 + interval '1' day;
date1 := date2 + interval '2' month;
date1 := date2 + interval '3' year;
It's almost the same but I prefer latter for better readability.

Resources