I have stored date in a column of android sqlite database in YYYY/MM/DD format . Now I want to use julianday function in sql query to select special rows of database however julianday uses date in YYYY-MM-DD format . How can I convert date format inline with sql query?
Please rewrite this sample query which "startdate" and "enddate" are stored in YYYY/MM/DD format not YYYY-MM-DD:
select * from events where julianday(enddate)-julianday(startdate)>1200
SQL query accepts inline replace:
select * from events where julianday(replace(enddate,'/','-'))-julianday(replace(startdate,'/','-'))>1200
for more complex conversion use prepared SQL convert() function
Related
I have a database with 5 columns and one of the column is date string of the format "dd/mm/yyyy hh:mm:ss". I want to fetch the data from DB based on the earliest date first.
Below is the query that I have used :
#Query("SELECT * from database_table ORDER BY dateTime('%d/%m/%Y %H:%M:%S',date) DESC")
fun getAllTransactions(): List<DataBaseItem>
With the above query I am receiving the data as it is and not as per the specified format.
Please let me know how to achieve this without changing the database structure.
The SQLite datetime function expects the first parameter to be a date, and the subsequent parameters to be valid modifiers as per:-
date(time-value, modifier, modifier, ...) see https://sqlite.org/lang_datefunc.html#overview
You are instead passing a format as the first parameter and the value from the column as a modifier (which it isn't).
The datetime returns a specific format i.e. YYYY-MM-DD HH:MM:SS.
IF you want a date to be returned as dd/mm/yyyy then you would have to use the strftime function so strftime('%d/%m/%Y %H:%M:%S',date). However, date MUST be in a format recognised by SQLite.
If the date is stored as dd/mm/yyyy then it is not a recognised format.
see https://sqlite.org/lang_datefunc.html#time_values
As such you need to format it accordingly to suit the ORDERing so
ORDER BY substr(date,7,4)||substr(date,4,2)||substr(date,1,2) DESC
see https://sqlite.org/lang_corefunc.html#substr
NOTE this assumes 1st March 2020 is 01/03/2022 that is leading 0's. If there are no leading 0's then the reformatting is more complex.
So I'm trying to query all the same dates but this code won't work
SELECT * FROM schedTBL WHERE CONVERT(varchar, DueDateTime, 120) LIKE " + "'%"+ text + "%'"
it returns no such column:varchar
This looks like a SQL statement for SQL Server, not SQLite...
Assuming you are storing dates in field DueDateTime without the hour part, something like this should do:
SELECT * FROM schedTBL WHERE DueDateTime = '2016-09-24'
Or you'll need like if you also have hour values. That depends on how you are actually storing dates/times in your SQLite DB.
Try formatting the date in Java and send it as a parameter to your query.
SQLite doesn't handle dates like SQL Server.
See:
SQLite Date And Time Functions
In SQLite, date values would be formatted with the strftime() function, or for this particular format, with the date() function:
... WHERE date(DueDateTime) LIKE ...
But this works only for supported date formats.
If the values in your database use an unsupported format, your only choice is to convert them, or to try to match the actual string:
... WHERE DueDateTime LIKE ...
I am trying to do some datetime aritmethics in Sqllite. I have a table with two rows defined as type DATETIME. These fields are called LOG_FROM and LOG_TO. In a query on this table I try to do the following:
SELECT SUM(LOG_FROM - LOG_TO) AS TIMESPENT FROM LOG_TABLE WHERE X=Y;
This subtraction inside the SUM function does not work as intended. It will always return the number 0. I read the data via a cursor.getString.
When I store dates in the table I convert them to strings on the format yyyy-MM-dd HH:mm:ss
SQLite ignores column data types. Your date strings are strings.
Your date format is supported by the built-in date functions, so you can use them to convert the dates to numbers.
Assuming that you want the result to be a number of days, use julianday:
SELECT SUM(julianday(LOG_TO) - julianday(LOG_FROM)) ...
I have date in string format in the table in yyyy-mm-dd format.
I want to convert that string to date format for further comparision on the date.
Any help would be greatful..
I'd suggest you to use SQLite - DateTime functions to compare field data value. Alternately you can use java.text.SimpleDateFormat class's parse method to convert string date to Date type.
I got the query of this.
SqlLite compares difference between two string date directly.
select column from Table where columnDate between '2012-07-01' and '2012-07-07'
But thanks for your interest guys.
I am trying to retrieve data from the database with the following query:
select * from Oil Where date(DDate)=date('2012-08-07');
where DDate is one of the column in the Oil table with datatype text.I get the Arrayindexoutofboundsexception. I found that it is not returning any value the count is 0.
can anyone help me with this.What i am actually trying is to do is store date value in the table.I couldn't find which datatype should be used to save date,from the sqlite site is that it doesn't have a particular datatype for date and time.
SQL Server comes with the following data types for storing a date or a date/time value in the database:
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MM:SS
SMALLDATETIME - format: YYYY-MM-DD HH:MM:SS
select * from Oil Where date(DDate)=date('2012-08-07');
can be
select * from Oil Where DDate='2012-08-07';
If you have DDate in the date format 'yyyy-MM-dd' then you can directly compare the date using
select * from Oil Where DDate='2012-08-07';
else if you have DDate in the date format 'yyyy-MM-dd HH:mm:ss' then the query looks like this
select * from Oil Where date(DDate)='2012-08-07';
If you are saving date in sqlite database and you have to perform date operations like sorting or using date() and time() functions then you have to use the 'yyyy-MM-dd HH:mm:ss' date format.
Datatype doesn't matter for sqlite.