select * from table_my where my_date between '2014-07-23 00:00:00' and '2014-07-30 00:00:00' order by _id desc
Here is my query, I d like to create this simple date compare query, but not working. This query don't add anything out. Why? How can I compare dates in sqlite?
Try below query using strftime date functions.
SELECT * from table_my
WHERE strftime('%Y-%m-%d', my_date )
between
strftime('%Y-%m-%d', '2014-07-23')
and
strftime ('%Y-%m-%d', '2014-07-30');
EDIT:
To use with hours and minutes, you can try something like:
SELECT * from table_my
WHERE strftime('%Y-%m-%d %H:%M', my_date )
between
strftime('%Y-%m-%d %H:%M', '2014-07-23 11:22')
and
strftime ('%Y-%m-%d %H:%M', '2014-07-23 11:25');
select * from table_my where my_date between '2014-07-30' and '2014-07-23' order by _id desc
use above query may it will work to you i also got such kind of problem during coding i think it is coming cause your using lower value before greater value
may it will help you just chek it
i just swiped the two dates also i removed timestamp
Related
I have SQLite database in Android app with records like sample below
id date
1 2023-01-29T15:56:00.733533+01:00
2 2023-01-29T15:56:02.092214+01:00
I need to filter these records by days, month, etc. I found that strftime function should the best way to do this, but my SELECT is not working. Can someone please help what I'm doing wrong.
My SELECT is
SELECT * FROM history WHERE date = strftime('%Y-%m-%d', '2023-01-29');
Thank you.
The time is stored in ISO-8601 standard format
Do this instead
SELECT * FROM history WHERE strftime('%Y-%m-%d', date)='2023-01-29'
//for a particular month
SELECT * FROM history WHERE strftime('%m', date)='2'
With strftime() you can format your timestamps to a specific format and the correct syntax is this:
SELECT *
FROM history
WHERE strftime('%Y-%m-%d', date) = '2023-01-29';
or, for a specific month like '2023-01':
SELECT *
FROM history
WHERE strftime('%Y-%m', date) = '2023-01';
or, for a specific year like '2023':
SELECT *
FROM history
WHERE strftime('%Y', date) = '2023';
I am trying to pull last 31 days data from SQLLite db, using below SQL. I already searched google and tried various options, while all SQL works i am not getting results as expected. For example in below screenshot you will see i do have record on 18-Sep but sql doesn't return any results...I am not sure what i am missing here..
SELECT * FROM transactions WHERE TIMESTAMP > (SELECT DATETIME('now', '-30 day'))
Use between instead of Timestamp comparison directly -
SELECT * FROM transactions WHERE TIMESTAMP BETWEEN datetime('now', '-31 days') AND datetime('now', 'localtime')
This is because SQLlite stores dates in String format and doesn't have a standard DateTime format. So The direct comparison of String would fail. Hence using of the datetime function to compare dates with existing values helps.
You can also try as
SELECT * FROM TRANSACTIONS WHERE TIMESTAMP > datetime('now', '-30 days')
I have a SQLite table that has one column for an amount of money spent, and one column for when (YYYY-MM-DD) that money was spent. Both columns are strings. How do I write a query that will give me the total amount that was spent this month?
The closest I've come is
select sum(expenseValue)
from expenses_table
where CAST(substr(date,6,2) AS INT) == strftime('%m', 'now ')
which is not an error but also does not produce any value.
Don't do the comparison as integers, do the comparison as strings:
select sum(expenseValue)
from expenses_table
where substr(date, 6, 2) = strftime('%m', 'now')
What might be happening is that "6" (as an integer) is not equal to "06" as a string.
If your date is in the format yyyy-mm-dd, you could use date modifiers to do calculations on dates easily:
SELECT * FROM expense WHERE date > date('now', 'start of month');
In general, you can issue simple queries as such:
SELECT * FROM expense WHERE date > '2016-07-01';
See https://www.sqlite.org/lang_datefunc.html
Apparently strftime('%m%', 'now') returns 07 even though the current month is June. So the correct query for the current month is
select sum(expenseValue)
from expenses_table
where substr(date, 6, 2) = strftime('%m','now', '-1 month').
Thanks anyway to Gordon!
I am storing some data and date that is selected by user in sqlite in dd-MMM-yyyy format in storeDate named column with TEXT type because afaik there is no DATE type directly. Now I want to retrieve the stored data month wise. Suppose I give input as 01-Dec-2013 and build where clause, then I need to get the data of that particular month(Dec, 2013). Can someone Please suggest how to achieve this? My current query is:
Cursor cursor= db.rawQuery("SELECT * FROM "+TABLE_NAME+" "+whereClause+" GROUP BY "+CATEGORY, null);
What should be given at the place of whereClause?
SQLite does not support month names, sadly. You will have to convert it to a month name either using a lookup table, a case statement, or a switch on the presentation layer.
Try this
SELECT strftime('%Y-%m', table_column) FROM `table name` WHERE strftime('%Y-%m', table_column) = '2013-04'
If you want to print the month name only then try this
SELECT case strftime('%m',date_column) when '01' then 'January' when '02' then 'Febuary' when '03' then 'March' when '04' then 'April' when '05' then 'May' when '06' then 'June' when '07' then 'July' when '08' then 'August' when '09' then 'September' when '10' then 'October' when '11' then 'November' when '12' then 'December' else '' end
as month FROM `your_table` WHERE strftime('%m',date_column)='12'
Also take a took at this stuff for more date and time functions in SQLite
Date And Time Functions
You could use MySQL's MONTH() function
SELECT * FROM tbl WHERE MONTH( trans_date ) = 3
I have a table with a column named call_date in the DATETIME format.
In this column I have the following date in this format yyyy-mm-dd.
What I want is to select the rows ordered by the month number in the column's value String.
Is there such SQLite function that does that??
I already tried strftime('%m', call_date) but ditn't work.
Thanks.
select * from myTable order by CAST(strftime('%m', call_date) AS INTEGER);
You can use strftime to get the month number and then order by that:
select ...
from your_table
order by strftime('%m', call_date)
In general I would try to avoid to order a result set by a calculated expression due to possible performance penalty. I would use an alternative approach: for example I would add MONTH column into the table and define a TRIGGER which will set the new column value. Then in queries you can order by a new column.