SQLite3 DateTime comparison in linux - datetime

As per seen in image my first query return 5 rows but my second query does not return any rows.
It shoud be return 3 rows.
I also have tried with
Store my all datetime data in format of 'yyyy-MM-dd hh:mm:ss'
"SELECT billheaderid,billheadercode,billtotalitem,billtotalamount,createdby,createdon WHERE cretedon >= Datetime('2014-08-19 12:26:32')"

Date values with "AM/PM" fields cannot be compared correctly with string comparisons
(1 is larger than 0).
You have to change all the values in the database to the correct format yyyy-MM-dd hh:mm:ss.
(And it is not necessary to call the datetime function.)

Store your data in form of
'yyyy-MM-dd hh:mm:ss'
And please care that '2014-08-19 03:45 PM' must be store as '2014-08-19 15:45:23' not as '2014-08-19 03:45:23'.
After that you don't need use datetime function. I am sure it'll work 100%.

Related

Using a query with a datetime cell in spreadsheets

I need to query a cell that contains a datetime but its formatted by default when I import it to only show the date.
31/7/2020 19:18:58 (in reality it's like this)
31/7/2020 (but it shows this)
So when I run this query:
=QUERY(A5:R10, "select K")
It returns only the date no matter what I do:
31/07/2020
I've tried:
Options
Formatted like "yyyy-MM-dd HH:mm" it returns 00:00
Filter by datetime or timestamp
Used '"&Text(now(), "yyyy-mm-dd HH:mm"&"' or something like that
The question is:
Is there a way to do what I'm trying to do without reformatting the imported cells?
link to test it:
https://docs.google.com/spreadsheets/d/14rGPngGvFXGP8txMS2yFo1v4gPcI4K1oYWj_8yt2Uq8/edit?usp=sharing
when I select one cell with F2 it shows the time:
Thanks a lot for your time!
select column B and format it as date time
or without reformating it:
=ARRAYFORMULA(QUERY(1*(B2:B4&""), "format Col1 'yyyy-mm-dd hh:mm:ss'"))
or:
=ARRAYFORMULA(QUERY(TEXT(B2:B4, "yyy-mm-dd hh:mm:ss"), "select *"))
Just make sure your cells are formatted as Automatic
If not, even if you use the format clause of the query, the clause will be ignored

Sqlite date between

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;

In Teradata, trying to convert Timestamp(6) to timestamp(0)

A is in the format of timestamp(6). I need it in timestamp(0). The code I am using is the following:
cast(cast(A AS date) as timestamp(0))
FROM 'table'
where A >= '?StartDT'
After inputing the date I want for the parameter I get the 'Invalid timestamp' error.
If A is truly a Timestamp(6) then casting it first as a DATE will affectively trim off the time elements, so when you cast the result to a TIMESTAMP(0) you are going to end up with a time of 00:00:00.
You'll need to also cast the TIMESTAMP(6) field as a time and then add the results together like:
CAST(CAST(A AS DATE) AS TIMESTAMP(0)) + (CAST(A AS TIME(6)) - TIME '00:00:00' HOUR TO SECOND)
You can also use SUBSTRING() to snip off the last 6 characters of the TIMESTAMP(6) field and cast that resulting string to a TIMESTAMP(0):
CAST(SUBSTRING(CAST(A AS CHAR(26)) FROM 1 FOR 19) AS TIMESTAMP(0))
This doesn't address the INVALID TIMESTAMP error you are getting though. Are you certain that field A is a TIMESTAMP(6) and not a VARCHAR() that looks like a Timestamp? What happens when you remove the outer cast, are their any dates in the result that look like they wouldn't convert nicely to a timestamp? Something is not quite right here, and I suspect that it's in your data.

Checking if a Date falls within a Range of Dates in SQLITE

I have an sqlite table with with 2 date columns (beginning & end). I am looking for a query that returns the date range a date falls in. e.g Checking range where 10-02-2016 falls given 2 records as follows
(01-02-2016 - 12-02-2016) & (13-02-2016 - 20-02-2016)
Clearly it falls in the first range but how do I do it in SQLITE. Please help?
I think the problem is next SQLite requires dates to be in format YYYY-MM-DD. With that format you could write a simple query:
select * from table where yourdate between begin and end
If you can't change your format you should look at next sqlite functions https://sqlite.org/lang_datefunc.html
Also you can try substr function to cotact date in needed format.

I need a sqlite equivalent of the folling msaccess query

Select distinct Format(DateAdd(""s""," & columnname & ",""1/1/1980 12:00:00 AM""), 'dd-MMM-yyyy') as A
I have assumed that the seconds to add and the original date are hard coded values below whilst awaiting clarifications requested in the comments.
To add a number of seconds to a date you can use:
select datetime('1980-01-01 00:00:00', "345000 seconds");
This gives the result: 1980-01-04 23:50:00
The example above is just under 4 days in seconds, if you want to truncate the result to just the date as implied by the query in your questions then you can wrap this inside a date function. However, this would give the result in the format "YYYY-MM-DD" rather than "DD-MMM-YYYY" as your access query does.
Unfortunately I cannot find any native SQLite function to convert a numeric month value to mmm format. You can do this manually with replace (similar to the answer to this question), but this is a bit messy.
If you are happy to live with the numeric months then you can simply use:
select strftime('%d-%m-%Y', '1980-01-01 00:00:00', "345000 seconds");
This gives the result: 04-01-1980
More information on the SQLite date / time functions can be found here.

Resources