can you please help me why this code does not work?
I dont understand why the result include "2017".
SQLLITE
QUERY
SELECT issue_date as count FROM tablename where issue_date >= "08/08/2016" and issue_date < "09/01/2016"
Result
"08/08/2017"
"08/11/2017"
"08/18/2017"
"08/18/2017"
"08/22/2017"
"08/22/2017"
"08/28/2017"
"08/31/2017"
Create query
CREATE TABLE tablename (
issue_date datetime text not null
}
Insert query
INSERT INTO tablename (issue_date) values ("08/31/2017");
You are storing your dates in a non ANSI compliant format. As there is no formal date type in SQLite, and all dates are essentially stored as strings, your current date comparison will behave and sort as if you are comparing to text. It won't work, because you have the month first, followed by the day, followed by the year. To get text comparisons of dates to work correctly, use a format something like this:
yyyy-mm-dd
You should change the format you use to store dates, but one workaround would be to build the issue date in the correct format and then do the comparison, also against a date string in the same correct format:
SELECT
issue_date AS count
FROM tablename
WHERE
SUBSTR(issue_date, 7, 4) || '-' ||
SUBSTR(issue_date, 1, 2) || '-' ||
SUBSTR(issue_date, 4, 2) BETWEEN '2016-08-08' AND '2016-09-01'
SQLite's data types does not have a real DATETIME type. It only has NULL, INTEGER, REAL, TEXT and BLOB. Any other type is converted to these, so what it is doing in your case is storing those values as strings, and this comparing as strings.
I personally prefer to store the dates as UNIX timestamps (hence integers) to avoid complicating the SQL queries and simplify the whole thing (although the values in database will become less human-readable).
Because you specified the query with AND. So it matches your query. Use BETWEEN expression.
expression BETWEEN value1 AND value2;
Related
How can I convert timestamps to dates in SQLite?
The following code only produces a Timestamp column and a Date columns with NULL values. The the SQL code needs to convert from a "08/28/2020 8:00" format.
SQL CODE:
'''Select Timestamp, strftime('%m-%d-%Y', Timestamp) as Date
FROM Room_Data'''
The SQLite documentation is pretty clear, but I can't seem to get the desired result.
The strftime is meant to format a date, rather than perform conversion.
In the meantime you could try something like that to gather the pieces:
SELECT Timestamp,
SUBSTR(c,7,4) || '-' || SUBSTR(Timestamp,1,2) || '-' || SUBSTR(Timestamp,4,2) as Date
FROM Room_Data
Since SQlite doesn't really have the concept of a date, unlike other DBMS, the best choice would be to convert your dates to integer, either as Unix timestamps or in string form (YYYY-MM-DD) but storing dates as integer like 20201010 would be acceptable too.
NB: be careful with names like Timestamp or Date, they are reserved keywords in many programming languages and DBMSes.
The original code won't work in Windows 10 for some reason. Trying this from a Linux distro (Kubuntu, in this case) seems to resolve the issue. In Windows, the date needs to be converted to a '2020-01-01' format to actually work.
SQLite is amazing, but not sure why functionality changes for Windows 10. Feel free to comment if you know more about the differences.
In Cognos 10.2.1 (FP7), I have a situation with two tables (see below), where a given transaction may be in the same "PERIOD", but what I want to do is to only give me results for transactions that have a date after the 15th of the month (i.e - the transaction may be in PERIOD 201510, and I only want transactons on/after 10/15/2015). The PERIOD is an input parameter that the user selects - I want to build the date from a portion of the PERIOD. (Before anyone complains about syntax, etc. - I've tried to simplify this as much as possible).
Given Table A:
ID varchar
PERIOD varchar
Given Table B:
ID varchar
TYPE varchar
TRANDATE timestamp2
Query1
[A.PERIOD]=?PARM1? <- this is the user selected input parameter
Query2
[B.TYPE] in ('A','B')
Then create a join from the results of Query1 and Query2 on the A.ID=B.ID to give Results1
I've tried the following (I'm keeping this as simple as I can - so I'm ONLY working with the YEAR portion of the period):
[Results1].[TRANDATE] >= concat(substring([Results1].[PERIOD],1,4),'-10-15T00:00:00.000000000')
[Results1].[TRANDATE] >= cast(concat(substring([Results1].[PERIOD],1,4),'-10-15T00:00:00.000000000'), timestamp2)
In both cases - Cognos won't validate the expression, or, if it does validate, I get a runtime error when running the report. In both cases, I get messages basically "literal does not match format string", even with the cast.
So - how can I get pieces/portions of the parameter, and slice/dice them to use as a date comparison as I mentioned above?
Given: Column [PERIOD] of type integer in the form YYYYMM, [TRANDATE] of type date/time
[TRANDATE] >= cast(cast(cast([PERIOD]/100,integer),varchar(4)) || '-' || cast(mod([PERIOD],100),varchar(2)) || '-15',date)
Given: Column [PERIOD] of type string in the form 'YYYYMM', [TRANDATE] of type date/time
[TRANDATE] >= cast(substring([PERIOD],1,4) || '-' || substring([PERIOD],5,2) || '-15',date)
The trick is that you don't have to build a full date/time string. You only have to build a string of format 'YYYY-MM-DD' in order to cast to a date type.
In Node.js I created a DATETIME string for in my sqlite3 database using moment.js and I formatted it
moment().format('YYYY-MM-DD h:mm:ss')
Never needed that column until after this project and I want to do some calculations. Turns out I should have formatted using 'hh', or two decimals for the hour (eg. '09' instead of '9') because sqlite3 otherwise won't see it as a valid DATETIME entry. Now I have 20k entries with a tonload of invalid DATETIME entries, argh!
What would be an efficient way to update all the entries in this column to valid sqlite3 DATETIME entries? Is it possible through just SQL?
This may work:
UPDATE t SET d = SUBSTR(d,1,11) || '0' || SUBSTR(d,12) WHERE LENGTH(d) = 18;
This question already has answers here:
SQLite DateTime comparison
(14 answers)
Closed 9 years ago.
Im storing my dates as string in format 'dd-mm-yyyy'. Now I want to do a comparison like this:
SELECT strftime('%d/%m/%Y', myDate) as myDate
FROM myTable
WHERE myDate>='01/07/2013' and myDate<='24/07/2013'
But I get nothing. Whats wrong with this query?.
THIS IS NOT A DUPLICATE QUESTION. I was storing everything as String, no as a DATE or DATETIME. I was trying to do a comparison between strings with date format. So is not fair the down vote.
There is no way you can get your query to work like that with your current setup:
I'm storing the date as a String so I guess with the using of strftime I can get what I want. Or am I wrong?
Basically: You're wrong!
The problem here is that a string formatted like that will not be sortable in the way you want.
Specifically:
myDate>='01/07/2013' and myDate<='24/07/2013'
Here, any date that is between the first and the 24th of any month in any year will match this. Ie. this will match: "02/01/1900", as will this: "02/12/2099".
The reason for this is that string comparisons are alphanumerical, not numerical (or datewise). "02" is greater than "01" and less than "24", and the rest is just redundant.
The "logical" way to format a date as a string is to start with the most significant value and work your way downwards, ie. this format: "yyyy/mm/dd", and not the other way around, but the real logical way to store a date, is to store it as a date, and not as a string.
The only way to work with the strings is to convert each string to a date and work with that, but the best way is to fix your schema.
If you absolutely cannot change your current schema, here is a way to make it work, but I do not advice this:
where (substr(myDate, 7, 4) || '-' || substr(myDate, 4, 2) || '-' || substr(myDate, 1, 2)) between '2013-07-01' and '2013-07-24'
This will pick apart the strings, put them together again in the right order, before doing the comparison. Also note the format of the last two dates there.
Did you try this :
SELECT strftime('%d/%m/%Y', myDate) as myDate
FROM myTable
WHERE myDate between date('01/07/2013') and date('24/07/2013');
I am using SQLite for a project and < symbol is not working in the query.
There is a table named Holidays which has a field of datatype datetime.
Suppose the table contains some dates of current year in the column HolidayDate.
SELECT HolidayDate
FROM Holidays
WHERE (HolidayDate >= '1/1/2011')
AND (HolidayDate <= '1/1/2012')
The < symbol in the above query is not working. > symbol in the above query is working well.
Please help me.
Try:
SELECT HolidayDate
FROM Holidays
WHERE HolidayDate >= date('2011-01-01')
AND HolidayDate <= date('2012-01-01')
(date format must be YYYY-MM-DD)
There is no datetime datatype in sqlite.
Sqlite only has 4 types:
integeral number
floating-point number
string (stored either as utf-8 or utf-16 and automatically converted)
blob
Moreover, sqlite is manifest-typed, which means any column can hold value of any type. The declared type is used for two things only:
inserted values are converted to the specified type if they seem to be convertible (and it does not seem to apply to values bound with sqlite_bind_* methods at all)
it hints the indexer or optimizer somehow (I just know it has trouble using indices when the column is not typed)
Even worse, sqlite will silently accept anything as type. It will interpret it as integeral type if it starts with "int", as string if it contains "char" or "text", as floating-point number if it is "real", "double" or "number" and as blob if it's "blob". In other cases the column is simply untyped, which poses no problem to sqlite given how little the typing means.
That means '1/1/2011' is simply a string and neither dates in format 'mm/dd/yyyy' nor dates in format 'dd/mm/yyyy' sort by date when sorted asciibetically (unicodebetically really).
If you stored the dates in ISO format ('yyyy-mm-dd'), the asciibetical sort would be compatible with date sort and you would have no problem.