Incrementing date column in sqlite3 - datetime

I have a sqlite3 table with dates stored as text not null with the format MM-DD-YY, and have been doing most of my manipulation in Java by iterating through the cursor, however, it's proving to be a bit ugly/cumbersome, and I was wondering if there's any way to do an update statement in sqlite3 to increment/decrement all records by a certain number of days. I'm mostly having trouble figuring out how to convert my text column into a format that I can use with something along the lines of datetime(myDate, '+1 Day');. Can anybody point me in the right direction? Thanks guys!!

The documentation of the built-in date functions documents the supported date format:
YYYY-MM-DD
You should store your date values in this format.
If you cannot do this, you can use substr() to extract the fields of the date and convert it into the supported format.

Related

Two datetime formatting strings in one vector

i have a large structure dataset.
each field in the structure is a XXXX*4 cell. the XXXX are because they are not constant in size. the first column is datetime.
the problem is that part of the vector is in the format of: '1/9/2015 00:00:00' that is dd/MM/yyyy HH:mm:ss
while the other part is in the format of '1/9/2015 00:00' that is dd/MM/yyyy HH:mm.
this change can happen more then once in each date vector.
is there any way to call the datetime function with two format types? or a general one that covers both of these?
for lack of a better option i would scan each row, and fix it, but it would take a lot of time. hope someone can help... thanks.
well, i just made a for loop, and wrote this inside:
s=fieldnames(DataSet);
for i=1:length(fieldnames(DataSet))
for j=2:(length(DataSet.(s{i})))
if length(DataSet.(s{i}){j,1})>=17
DataSet.(s{i}){j,1}=DataSet.(s{i}){j,1}(1:(length(DataSet.(s{i}){j,1})-3));
end
end
end
it worked. actually pretty fast, only took 7 seconds (i was a bit surprised)
notice it only works to change from 'dd/MM/yyyy HH:mm:ss' to 'dd/MM/yyyy HH:mm'.
but i guess you can manipulate it to adjust most types.

Sqlite format date from hour to hour

I want store in my sqlite database date in format DD/MM/YYYY HH:MM-HH:MM there is any solution of this ? I found only that YYYY-MM-DDTHH:MM answer.
SQLite has no specal data type for dates.
You can store dates in any format you want, but if you want to use any of the built-in date functions, you have to use one of the supported date formats.
What you want to store is not a date (a point in time), but a time interval.
There is no built-in support for intervals; the best you can do is to store the start and the end of the interval in two separate columns.
Please note that storing a date and displaying a date are two different things.
It would be easier to compute the length of an interval when the two date values are stored in one of the numeric date formats.

R date conversion to POSIX format in Tableau

I am trying to connect R-Impala-tableau.
I have an R code scripted in tableau calculated field which runs on a date field.
R code is:
script_str('date<- as.POSIXct(strptime(.arg1,"%m%d%y"))',attr([date_val]))
I want this code to run on the date values extracted from impala.
The date_val here is extracted from impala and its converted to date type in tableau.
However, the above code returns NULL. I also tried keeping the date_val field to string, date_time, numeric data types.
I am doing this to perform a cut operation on the dates obtained(eg:intervals<-cut(date,"1 day")). To do this, I need the field date in numeric format.
I even tried script_str('date<- as.POSIXct(strptime(as.character(.arg1),"%m%d%y"))',attr([date_val]))
Has anybody faced similar issue?
Any help in this regard will be appreciated.

SqlLite select query for between date range with date format mm-dd-YYYY

I'm using SQLite, I want to get all the students who are having their DOB between 01-01-2000 and 12-12-2010. The dates are in TEXT and mm-dd-YY format in my database. I've done like this:
SELECT * FROM student_details
WHERE strftime('%m-%d-%Y', studentDob)
BETWEEN strftime('%m-%d-%Y', '01-01-2000')
AND strftime('%m-%d-%Y', '12-12-2010')
But it is not giving any result even though the records are there in my DB.
Please help me if there any wrong in my query. It not even producing any error.
The American date format is inherently unsortable because units are not arranged in consistently increasing or consistently decreasing order. An ISO (Japanese) style date, however, can be sorted alphabetically making it the most superior of human-readable (and computer sortable) forms.
In the absence of a practical date format you have to convert that text string into something sortable, e.g. a Japanese form text string, or an integer.
For SQLite see Date and Time Functions.
For parsing date strings in SQLite see this StackOverflow answer.

Delete old records

I have a column in my sqlite table which is string and has the following format
2011-09-06 18:34:55.863414
You can see that it identifies date and time. I'd like to construct a query that will
delete all records that are older than certain date and time.
Is this possible?
Since your date is already in the best format (largest time-period values to smallest)
DELETE FROM myTable
WHERE myDateField < '2011-09-06 18:34:55.863414'
BTW -- dates are strings in sqllite, AFAIK (which is why the format matters -- biggest values to smallest, so it works alphabetically too). IF you want to treat them as dates, you can use functions. Some good examples here: http://sqlite.org/lang_datefunc.html
DELETE FROM tablename WHERE columnname < '2011-09-06 18:34:55.863414'
See:
http://www.sqlite.org/lang_datefunc.html

Resources