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.
Related
In my application i am trying to fetch records from a sqlite table with respect to a range of dates selected from the date picker. The records in the table are as follows :
The query formed is as given below :
select * from Order_Master where Order_Date >= '12-04-2015' and Order_Date <= '11-03-2016' And WSS_Code = '1014332'
This query does not return any value which is not the desired result as the dates are in the selected range.
What could possibly be wrong here ? Am i missing something?
Kindly guide me through this. Thanking you in Advance !
Your date format cannot be used for comparisons, because strings are compared lexicographically, i.e., with the first characters compared first.
In this query, you are searching for dates with a month that is at least 12 and, at the same, no larger than 11.
SQLite has no separate data type for dates.
To store dates in a database, you have to choose one of the existing data types (number or text).
When using SQLite's built-in date functions, you must use one of the formats supported by them.
Try this , hope it helps
Change your datatype to text
SELECT * FROM Order_Master WHERE Order_Date BETWEEN '12-04-2015' AND '11-03-2016' AND WSS_Code = '1014332';
As well as your query will also work.
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
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 problem in displaying the data in my application for each week?
I can show the data by day using this code in my SQLite
SELECT substr(_id, 1, 7) as _id, sum(value) as total FROM pd_table GROUP BY _id order by _id desc
I was also able to display the data based on the month by using this code
SELECT substr(_id, 1, 10) as _id, sum(value) as total FROM pd_table GROUP BY _id order by _id desc
_id using SimpleDateFormat ("yyyy-MM-dd HH: mm: ss")
now i want to display it every week. How?
Any answer is very useful for me :)
SQLite has internal date/time formatting function strftime, and one of possible format option is a week number. So your query should look like this:
SELECT strftime('%Y-%W',_id) as week_of_year, sum(value) as total
FROM pd_table
GROUP BY week_of_year order by _id desc
Though I didn't try it on Android. See more here: http://sqlite.org/lang_datefunc.html
I have a table filled with data for given dates. Sometimes I do not have a row for a given day because I might not have data for that day. Now when I query to get the results I would like to have the result set include a row for every date from the earliest date to the latest. Is there a way to form a query that will generate a default row when a date is missing in the results set without actually inserting them in the database? The date column is in the format YYYY-MM-DD.
No matter what the data type, you can create a default value for any column in your table. Please read about the SQL DEFAULT Constraint.
CREATE TABLE Foo (
_id INTEGER PRIMARY KEY,
date TEXT DEFAULT '2012-08-17');
Or if you want to return a default value every time you query your database, use IfNull():
SELECT IfNull(date, '2012-08-17') FROM Foo;
(I have assumed that your date is text, however you can easily change this.)