How to change date format in sqlite database - sqlite

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

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.

SQLite3 seems to be ignoring ORDER BY ASC / DESC

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)

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

How can i convert datetime to date format in sqlite selected vaues

I just try to convert datetime to date, but without success. Help me to do it:
select date('startTime') from usertable ;
select datetime(substr(startTime, 7, 4 )) from usertable;
Just try this.. it will convert datetime to date.
SELECT strftime('%d-%m-%Y', fieldname)
SELECT strftime('%d-%m-%Y', 'now') gives output 5-06-2015

Comparing two date of varchar and getdate

I have a database table field named User_Created_Date of type Varchar.I want to write a query to fetch all records where the difference between Today's date and User_Created_Date is greater than 31 days
pls help
Since your VARCHAR column's date format is DD/MM/YY, use:
select * from Your_Table
where DATEDIFF(day, CONVERT(datetime, User_Created_Date, 3), GETDATE()) > 31;

Resources