SQlite REPLACE issue - sqlite

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.

Related

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

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));

Change DATE FORMAT from 30.9.2014 to 2014-09-30 or reverse

I want to join two tables in SQLiteStudio on column date, but I have different date format in each table. I have tried with
strftime('%Y-%m-%d', date)
but that obviously returned wrong date. For example, from 30.9.2014 I want to get 2014-09-30 and not 2014-9-30. My table is big around 5GB in csv file. Someone know how to do this?
I wrote code for this if someone needs it.
update table
set prc_dtm = case
when substr(prc_dtm,3,1)= '.' and length(prc_dtm)=9
then substr(prc_dtm,-4)||'-0'||substr(prc_dtm,4,1)||'-'||substr(prc_dtm,1,2)
when substr(prc_dtm,2,1)= '.' and length(prc_dtm)=9
then substr(prc_dtm,-4)||'-'||substr(prc_dtm,3,2)||'-0'||substr(prc_dtm,1,1)
when substr(prc_dtm,2,1)= '.' and length(prc_dtm)=8
then substr(prc_dtm,-4)||'-0'||substr(prc_dtm,3,1)||'-0'||substr(prc_dtm,1,1)
when substr(prc_dtm,3,1)= '.' and length(prc_dtm)=10
then substr(prc_dtm,-4)||'-'||substr(prc_dtm,4,2)||'-'||substr(prc_dtm,1,2)
end;`
The ISO format has fixed field lengths, so it is easier to convert it into the other format:
CAST(substr(date, 9, 2) AS num) || '.' ||
CAST(substr(date, 6, 2) AS num) || '.' ||
CAST(substr(date, 1, 4) AS num)

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

Resources