SQLite3 seems to be ignoring ORDER BY ASC / DESC - sqlite

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)

Related

SQlite REPLACE issue

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.

Retrive date wise data from SQLITE database kotlin

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

Why does '%d-%m-%Y' format return always NULL?

I have dates in this format 31/01/1970 or %d/%m/%Y.
This expression returns NULL
SELECT strftime('%d/%m/%Y','31/01/1970')
I thought it might have been a problem with the / character but also
SELECT strftime('%d-%m-%Y','31-01-1970')
returns NULL.
The only expression that seems to work is
SELECT strftime('%Y-%m-%d','1970-01-31')
Why?
PS: Actually from the sqlite3 console I get a syntax error.
You misunderstand how strftime works. strftime expects its time argument to be in ISO8601 format (i.e. YYYY-MM-DD for a date) and then it formats that time using the format string.
strftime isn't for parsing dates and times, it is for formatting them.
If you want to parse '31/01/1970' into a format that SQLite can work with then you're stuck with substr:
select substr('31/01/1970', 7, 4) || '-' || substr('31/01/1970', 4, 2) || '-' || substr('31/01/1970', 1, 2);
and then you could feed that string into strftime to format it:
select strftime('%d/%m/%Y', substr('31/01/1970', 7, 4) || '-' || substr('31/01/1970', 4, 2) || '-' || substr('31/01/1970', 1, 2));

How to change date format in sqlite database

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

Get days until future event in SQLite

Trying to get several things from a SQLite table with names and dates of birth and am having trouble getting the # of days until a person's next birthday. Dates are stored as SQLite's TEXT data type in format '%Y-%m-%d 00:00:00'.
I can get age:
SELECT ((strftime('%s', 'now') - strftime('%s', dob)) / 31536000) AS age
I like this solution for showing the closest birthdays first:
ORDER BY SUBSTR(date('now'), 6) > SUBSTR(dob, 6), SUBSTR(dob, 6) ASC
But I'm breaking my brain over getting the days until the next birthday. My latest attempt is taking the julianday of the substring of the day and month from dob and concatenate it with the current year to compare against julianday() and put in conditionals to take the year change into account, but I haven't worked that out yet and I'm hoping someone has a more elegant solution.
Have made my hideous solution work, so here it is:
SELECT
CASE WHEN
julianday((SUBSTR(date('now'), 1, 5) || SUBSTR(dob, 6, 5))) > julianday('now')
THEN CAST(ROUND(
julianday((SUBSTR(date('now'), 1, 5) || SUBSTR(dob, 6, 5))) - julianday('now'), 0) AS INTEGER)
ELSE CAST(ROUND((
julianday(SUBSTR(date('now'), 1, 5) || '12-31') - julianday('now')) + (
julianday(SUBSTR(date('now'), 1, 5) || SUBSTR(dob, 6, 5)) - julianday(SUBSTR(date('now'), 1, 5) || '01-01')), 0) AS INTEGER)
END
AS dub FROM person;
Will only have to put in another conditional to improve the rounding.

Resources