I have one SQlite database where i store some data with date.
Now i want to get data date wise like:
Month wise - it means i pass the value like Current date is EndDate to this month 1st date.
Year wise - it means i want to get data 1st-april-20..previews to 31-march-20..current
Start and End date wise - hear is which i pass date.
For that i got one solution is HERE for java.
But i have no idea this HOW TO WORK. Anyone please explain me how to work this and How to i get data as i mention above. FOR KOTLIN
TABLE
db.createTable(customerDetail, true,
credit_id to INTEGER + PRIMARY_KEY + AUTOINCREMENT,
credit_type to TEXT,
c_id to TEXT,
credit_amount to TEXT,
credit_date to TEXT,
addFrom to TEXT
)
UPDATE
For Month wise data i'll try below query like:
"SELECT * FROM $customerDetail WHERE $credit_date BETWEEN strftime('%d-%m-%Y', '$startDate') AND strftime('%d-%m-%Y', '$currentDate')"
/*SELECT * FROM CustomerDetail WHERE credit_date BETWEEN strftime('%d-%m-%Y', '01/02/2019') AND strftime('%d-%m-%Y', '23/02/2019')*/
But it's give me arralistSize = 0.
After that i can write new query like:
"SELECT * FROM $customerDetail WHERE $credit_date BETWEEN '$startDate' AND '$currentDate'"
/*SELECT * FROM CustomerDetail WHERE credit_date BETWEEN '01/02/2019' AND '23/02/2019'*/
In this query data will return. But it's return all data without any filtering.
If anyone knows why this work like this please help me.
MY DATA LIST
Thanks in advance.
Solution :
Solution for MONTH wise
I just change date format "dd/mm/yyyy" TO "yyyy/mm/dd" and re insert all data.
AND Fire below QUERY :
"SELECT * FROM $customerDetail WHERE $credit_date BETWEEN '$startDate' AND '$currentDate'"
SQLite does not have a Date data type like other RDBMS's. It treats dates as TEXT.
So when it compares the credit_date column it actually does compare strings.
This would be fine if you stored your dates in the format YYYY-MM-DD and compared against the same format.
But if you store your dates in the format DD/MM/YYYY then you can't compare.
So the best solution would be to change the format of the column credit_date to YYYY-MM-DD.
If this is not possible then you have to transform the string value of the column like this:
substr(credit_date, 7, 4) || '-' || substr(credit_date, 4, 2) || '-' || substr(credit_date, 1, 2)
Now you can use this in a query like:
SELECT * FROM $customerDetail
WHERE substr($credit_date, 7, 4) || '-' || substr($credit_date, 4, 2) || '-' || substr($credit_date, 1, 2)
BETWEEN '$startDate' AND '$currentDate'
But you have to make sure that $startDate and $currentDate are also in the format YYYY-MM-DD
Related
I have a date in the format 'MM/DD/YYYY'. I need the month, day, and year to be separated by : or - for functions like strftime to work in sqlite. I'm attempting to use REPLACE but it won't work on '/'.
Any help is appreciated.
UPDATE table SET date = REPLACE(date,'/','-')
Even if you replace / with -, the date functions of SQLite will not work with your dates.
The only valid date format for SQLite is YYYY-MM-DD.
Update the column date to that format like this:
UPDATE tablename
SET date = SUBSTR(date, -4) || '-' || SUBSTR(date, 1, 2) || '-' || SUBSTR(date, 4, 2);
See the demo.
I have a database of sports games which includes the date of these these games in the format DD/MM/YYYY (not good I know) and I'm running the command:
SELECT InningsDate FROM Test WHERE InningsPlayer="Player Name" ORDER BY date(InningsDate) DESC
to sort by date of the innings which does sort the database, however it ignores the DESC at the end and will always show the data in the same order regardless of it I put ASC / DESC / nothing at the end.
Any help will be much appreciated.
The format DD/MM/YYYY is not comparable and you should not use it in SQLite which does not support formatting for dates that don't have the format YYYY-MM-DD.
A solution for your case would be:
ORDER BY substr(InningsDate, -4) || substr(InningsDate, 4, 2) || substr(InningsDate, 1, 2) DESC
but as a permanent solution consider changing the format of the dates:
UPDATE Test
SET InningsDate =
substr(InningsDate, -4) || '-' ||
substr(InningsDate, 4, 2) || '-' ||
substr(InningsDate, 1, 2)
I am working on statistics page of my app and trying to query data by date.
To get the date range, I use Calendar.Date
date_range = Date.days_after_until(start_date, end_date, true)
|> Enum.to_list
And it returns date list of dates and each date looks like "2017-04-07". So with the date I got from date_range, I tried to query but it triggers an error like below.
where cannot be cast to type Ecto.DateTime in query: from o in Myapp.Order,
where: o.created_date >= ^~D[2017-04-07]
For created_date field of Order, I made field like this,
field :created_date, Ecto.DateTime.
If I want to query by date, how can I query it?
Thank in advance.
It looks like you're trying to compare a date and datetime. You need to cast one of them to the other type so the comparison works, e.g. convert the datetime in the database to a date:
date = ~D[2017-01-01]
from p in Post, where: fragment("?::date", p.inserted_at) >= ^date
or convert the Elixir Date to NaiveDateTime:
{:ok, datetime} = NaiveDateTime.new(date, ~T[00:00:00])
from p in Post, where: p.inserted_at >= ^datetime
If you have a start and end date, you just need to add an and to either. You don't need to generate the whole list of dates using any library.
from p in Post,
where: fragment("?::date", p.inserted_at) >= ^start_date and
fragment("?::date", p.inserted_at) <= ^end_date
or
from p in Post,
where: p.inserted_at >= ^start_datetime and
p.inserted_at <= ^end_datetime
How do I change the format of a date in %d/%m/%Y or any other from this string 10/09/2016 in sqlite?
I have tried this but it is not working:
select strftime('%d/%m/%Y', '09-10-2016'),t_date from party_trans
try this query
select strftime('%d/%m/%Y',datetime(substr(t_date, 7, 4) || '-' || substr(t_date, 4, 2) || '-' || substr(t_date, 1, 2))) from party_trans;
I would suggest to store date as time stamp instead of string. Timestamp gives you more flexibility and accuracy than date. Convert your date into millisecond (timestamp) format and store it into your database as INTEGER type.
Or try this:
select strftime('%d/%m/%Y', '2016-10-09');
Store date into INTEGER type in database.
select strftime('%d/%m/%Y', date) from table
I have a table in Oracle call STATISTICS.
COLUMN NAME DATE TYPE
MODEL VARCHAR2(30 BYTE)
NEW_COUNT NUMBER
NEW_DATE DATE
OLD_COUNT NUMBER
OLD_DATE DATE
PRNCT_CHANGE NUMBER
Now I have sql that updates statistics table:
UPDATE STATISTICS
SET
OLD_COUNT = NEW_COUNT,
NEW_COUNT =
( -- semantic table --
SELECT COUNT(*)
FROM TABLE(SEM_MATCH(
'{
?s ?p ?o
}',
SEM_Models(MODEL),NULL,
SEM_ALIASES(SEM_ALIAS('','http://SEMANTIC#')),NULL))
),
OLD_DATE = NEW_DATE,
NEW_DATE = SYSDATE
WHERE MODEL = &MY_MODEL
;
Now, can I do this? Push the date from a new date to an old date before I update the new date?
I am also doing the same thing with the NEW_COUNT and OLD_COUNT...
It sounded logical but is ok to do this?
So I followed my own advice :) and it worked just fine. I am not sure if this is the best practice but it does the trick